Change an icon in an Android ListView entry when pressed -
i trying implement listview in android features rows icon , describing text. when user presses row, background color should change , icon should replaced shown in image below.
changing background no problem using selector. can't figure out simple way change icon. tried adding 2 icons overlapping each other:
<relativelayout android:layout_width="50dp" android:layout_height="50dp" android:padding="10dp" > <imageview android:id="@+id/general_list_item_icon" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaletype="centerinside" /> <imageview android:id="@+id/general_list_item_icon_active" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visibility="gone" /> </relativelayout>
then put drawable in each imageview , want switch visibility when user presses row. there possibility using selector similar 1 background?
also: since listview filled dynamically, using selector static drawable references not work.
you can use setchoicemode
listview.setchoicemode(listview.choice_mode_single);
call method before calling set adapter. , override getview
method of adapter. sample code.
public view getview(int position, view convertview, viewgroup parent) { if(convertview!=null){ imageview img = (imageview)convertview.findviewbyid(r.id.imageview1); if(mylist.isitemchecked(position)){ convertview.setbackgroundcolor(color.white);// here can set color. img.setimageresource(r.drawable.img1);//img1 stored in rawable folder. }else{ convertview.setbackgroundcolor(0); img.setimageresource(r.drawable.img2); } } return super.getview(position, convertview, parent); }
i hope give solution want. best. thank you.
Comments
Post a Comment