Change Color Of Selected Listview Item
I want to change color of list item when it will press For that I did like below, list_item_selector.xml Copy
list_item_selector.xml
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_focused="true"><shape><solidandroid:color="#66FFFFFF" /></shape></item><item><shape><solidandroid:color="#FF666666" /></shape></item></selector>
Solution 2:
You should be setting the selector to the row and not the listview itself.
Solution 3:
<itemandroid:state_activated="true"><shapeandroid:shape="rectangle"><solidandroid:color="#333333" /><paddingandroid:left="5dp"android:right="5dp" /></shape></item><item><shapeandroid:shape="rectangle"><solidandroid:color="#222222" /></shape></item>
Solution 4:
Try with a custom adapter this also helps you to have full control over your items and set a default item selected; listView XML and item XML have no special setup.
publicclassListAdapterextendsArrayAdapter<MyObj> {
privatefinalint layoutInflater;
private Context context;
private List<MyObj> items;
privateintmSelectedItem=0;
privateintTAG_UNSELECTED=0;
privateintTAG_SELECTED=1;
publicListAdapter(Context context, int resource, List<MyObj> items) {
super(context, resource, items);
this.context = context;
this.layoutInflater = resource;
this.items = items;
}
publicvoidselectItem(int position) {
mSelectedItem = position;
notifyDataSetChanged();
}
@OverridepublicintgetViewTypeCount() {
return2;
}
@OverridepublicintgetItemViewType(int position) {
returnposition== mSelectedItem ? TAG_SELECTED : TAG_UNSELECTED;
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
Viewv= convertView;
if (v == null) {
LayoutInflatervi= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layoutInflater, null);
}
MyObjmyObj= items.get(position);
TextViewtextView= (TextView) v.findViewById(R.id.title);
textView.setText(myObj.title);
inttype= getItemViewType(position);
if(type == TAG_SELECTED) {
v.setBackgroundColor(Color.parseColor("#1da7ff"));
textView.setTextColor(Color.parseColor("#ffffff"));
} else {
v.setBackgroundColor(Color.parseColor("#f8f8f8"));
textView.setTextColor(Color.parseColor("#474747"));
}
return v;
}
}
Then in your activity:
ListViewlistView= (ListView) findViewById(R.id.list_view);
ListAdapteradapter=newListAdapter(mContext, R.layout.item_layout, list);
listView.setAdapter(adapter);
adapter.selectItem(0); // Default selected item// Get selected item and update its background
listView.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) {
adapter.selectItem(position);
}
});
Solution 5:
use this
android:background="@drawable/list_item_selector""
Post a Comment for "Change Color Of Selected Listview Item"