android - How to Highlight search results in ListView -
i not understand how can highlight search results.
i tried of spannablestring
here not know how implement in case. write error in line mtextview.settext(spannable)
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.search); // listview data final string[] products = new string[] { getresources().getstring(r.string.t1_2), getresources().getstring(r.string.t1_3), getresources().getstring(r.string.t1_4), }; lv = (listview) findviewbyid(r.id.list_view); inputsearch = (edittext) findviewbyid(r.id.inputsearch); mtextview = (textview) findviewbyid(r.id.product_name); // adding items listview adapter = new arrayadapter<string>(this, r.layout.list_item,r.id.product_name, products); lv.setadapter(adapter); lv.setvisibility(view.gone); // once user enters new data in edittext need text // , passing array adapter filter. inputsearch.addtextchangedlistener(new textwatcher() { @override public void ontextchanged(charsequence cs, int arg1, int arg2, int arg3) { // when user changed text search.this.adapter.getfilter().filter(cs); textlength = inputsearch.gettext().length(); // adapter.clear(); if (textlength != 0) { lv.setvisibility(view.visible); (int = 0; < products.length; i++) { if (textlength <= products[i].length()) { spannable spannable = new spannablestring(inputsearch.gettext().tostring()); colorstatelist bluecolor = new colorstatelist(new int[][] { new int[] {}}, new int[] { color.blue }); textappearancespan highlightspan = new textappearancespan(null, typeface.bold, -1, bluecolor, null); spannable.setspan(highlightspan, 0,5 , spannable.span_exclusive_exclusive); mtextview.settext(spannable); } } } } @override public void beforetextchanged(charsequence arg0, int arg1, int arg2, int arg3) { // todo auto-generated method stub } @override public void aftertextchanged(editable arg0) { // todo auto-generated method stub } }); } }
Comments
Post a Comment