On selection, on focus, on touch of the items we can change the color of the item, by default android will show a color, we can change it as we like. For that we need this xml to be stored inside the color folder, here only we needmention the colors for each listeners
list_bg.xml
That list_bg.xml will be called in the customized list xml as background
list.xml
This one is the main xml for the Lister.java
main.xml
The Lister.java will be like this, the main thing is the list_bg.xml only
Lister.java
list_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="false" android:state_pressed="false" android:drawable="@color/grey" /> <item android:state_pressed="true" android:drawable="@color/blue" /> <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/blue" /> </selector>
That list_bg.xml will be called in the customized list xml as background
list.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="2dp" android:background="@color/list_bg"> <TextView android:id="@+id/post" android:gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="50dp" android:textSize="20sp" android:textColor="#D0640D" android:layout_toRightOf="@+id/bite_image" /> </RelativeLayout>
This one is the main xml for the Lister.java
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:smoothScrollbar="true" android:background="#fff" android:cacheColorHint="#fff" android:fastScrollEnabled="false" android:clickable="true" android:layout_marginBottom="36dp" /> </LinearLayout>
The Lister.java will be like this, the main thing is the list_bg.xml only
Lister.java
package com.list.viewer; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class Lister extends ListActivity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String[] captionArray = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"}; ItemsAdapter ItemsAdapter = new ItemsAdapter( Lister.this, R.layout.list, captionArray); setListAdapter(ItemsAdapter); } private class ItemsAdapter extends BaseAdapter { String[] items; public ItemsAdapter(Context context, int textViewResourceId, String[] items) { // super(context, textViewResourceId, items); this.items = items; } // @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.list, null); } TextView post = (TextView) v .findViewById(R.id.post); post.setText(items[position]); return v; } public int getCount() { return items.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } } }
Thanks
akm from cdacians(www.cdacians.com)
No comments:
Post a Comment