How to change color of a listview's item and keep it unchaged until another item clicked from that listview android? -
how change color of list-view , keep unchanged until item clicked list-view?
mainactivity.java
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.dialog); list<hashmap<string,string>> alist = new arraylist<hashmap<string,string>>(); for(int i=0;i<3;i++){ hashmap<string, string> hm = new hashmap<string,string>(); hm.put("iconimg", integer.tostring(drwbl_imgs[i]) ); hm.put("title",str_tit[i]); alist.add(hm); } // keys used in hashmap string[] = { "iconimg","title"}; // ids of views in listview_layout int[] = { r.id.img_listicon,r.id.txtvw_slidebar_listview}; // instantiating adapter store each items // r.layout.listview_layout defines layout of each item simpleadapter adapter = new simpleadapter(getbasecontext(), alist, r.layout.child_slidebar_listview_layout, from, to); // setting adapter listview listview.setadapter(adapter); } }
main_layout.xml
<listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/white" android:divider="@android:color/transparent" > </listview>
child_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear_listview_lay" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:orientation="horizontal" > <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" android:background="#808080" > <imageview android:id="@+id/img_listicon" android:layout_width="15dp" android:layout_height="15dp" /> <textview android:id="@+id/txtvw_slidebar_listview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left|center" android:text="" /> </linearlayout> </linearlayout>
you need give selector list item.
left_menu_list_seletor.xml:
<item android:drawable="@color/listitemselecteditembgcolor" android:state_activated="true" android:state_enabled="true"/> <item android:drawable="@color/listitemselecteditembgcolor" android:state_pressed="true"/> <item android:drawable="@color/listitemcolor"/> <item android:drawable="@color/listitemcolor" android:state_enabled="false"/> </selector>
you can specify colors in colors.xml file
add background attribute child_layout android:background="@drawable/left_menu_list_seletor"
dont forget add choice mode listview
listview.setchoicemode(listview.choice_mode_single);
Comments
Post a Comment