java - How to retain the background color of selected item in list view? -
i having listview , edittext in activity. on selection of item in list view able change background diff color using 'android:listselector'. now, move edit text. on move edit text background color of selected item in list view changes default. how prevent this?
<listview android:layout_width="wrap_content" android:layout_height="129dp" android:id="@+id/listview2" android:choicemode="singlechoice" android:listselector="#666666" android:layout_below="@+id/button7" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="34dp" /> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edittext" android:padding="-11dp" android:width="300dp" android:inputtype="number" android:textsize="34sp" android:editable="true" android:layout_below="@+id/listview2" android:textstyle="bold" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="163dp" android:hint="$" />
public class category_adapter extends baseadapter { private context context; private layoutinflater inflater; //initialize selectedposition private int selectedposition = -1; public category_adapter(context context, arraylist<category_items> items, string show) { this.context = context; this.inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); } @override public view getview(int position, view view, viewgroup parent) { // todo auto-generated method stub if (view == null) { view = inflater.inflate(r.layout.category_titles_n_sellers_list, parent, false); //set background selected position if (position == selectedposition) { view.setbackgroundcolor(context.getresources().getcolor( r.color.orange_list)); // view.setbackgroundcolor(color.parsecolor("#ffa500")); } else { view.setbackgroundcolor(context.getresources().getcolor( r.color.white)); // view.setbackgroundresource(r.color.white); } return view; } //get selected position activtiy public void setselected(int position) { selectedposition = position; } }
now in activity class :
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // set custom adapter method , set selectedposition listview position ((category_adapter) adapter).setselected(position); }
try this, me worked.
Comments
Post a Comment