android - How do I get MultiAutoCompleteTextView tokenizer similar to Facebook app? -
i creating application has 'to' field in facebook app's "new message" feature.
after selecting item drop down list, create imagespan , add multiautocompletetextview
. have used spacetokenizer
view . problem when click on backspace, cursor first moves empty space (i.e., space tokenizer
) , when click on backspace again, whole word gets deleted....i want delete whole word on first click of backspace facebook app...
here code spacetokenizer
multicontenttext.settokenizer(new tokenizer(){ public int findtokenstart(charsequence text, int cursor) { int = cursor; if(i>0){ log.d("textchar ",""+text.charat(i - 1)); } while (i > 0 && text.charat(i - 1) != ' ') { i--; } while (i < cursor && text.charat(i) == ' ' || text.charat(i - 1) == '\n') { i++; } return i; } public int findtokenend(charsequence text, int cursor) { int = cursor; int len = text.length(); while (i < len) { if (text.charat(i) == ' ' || text.charat(i - 1) == '\n') { return i; } else { i++; } } return len; } public charsequence terminatetoken(charsequence text) { int = text.length(); while (i > 0 && text.charat(i - 1) == ' ' || text.charat(i - 1) == '\n') { i--; } if (i > 0 && text.charat(i - 1) == ' ' || text.charat(i - 1) == '\n') { return text; } else { if (text instanceof spanned) { spannablestring sp = new spannablestring(text + " "); textutils.copyspansfrom((spanned) text, 0, text.length(), object.class, sp, 0); return sp; } else { return text+" "; } } } });
i using code create textview
in multi-contenttext
spannablestringbuilder ssb = new spannablestringbuilder(multicontenttext.gettext()); string c="text list"; textview textview = (textview) inflater.inflate(r.layout.chips_edittext, null); textview.settext(c); // set text int spec = measurespec.makemeasurespec(0, measurespec.unspecified); textview.measure(spec, spec); textview.layout(0, 0, textview.getmeasuredwidth(), textview.getmeasuredheight()); bitmap b = bitmap.createbitmap(textview.getwidth(), textview.getheight(),bitmap.config.argb_8888); canvas canvas = new canvas(b); canvas.translate(-textview.getscrollx(), -textview.getscrolly()); textview.draw(canvas); textview.setdrawingcacheenabled(true); bitmap cachebmp = textview.getdrawingcache(); bitmap viewbmp = cachebmp.copy(bitmap.config.argb_8888, true); textview.destroydrawingcache(); // destory drawable // create bitmap drawable imagespan bitmapdrawable bmpdrawable = new bitmapdrawable(viewbmp); bmpdrawable.setbounds(0, 0,bmpdrawable.getintrinsicwidth(),bmpdrawable.getintrinsicheight()); // create , set imagespan ssb.setspan(new imagespan(bmpdrawable),0 ,c.length() , spannable.span_exclusive_exclusive); // set chips span multicontenttext.settext(ssb); multicontenttext.setselection(multicontenttext.gettext().length());
i not sure whether space tokenizer
right option type of behavior...any or pointers grateful...
here screenshot better understanding....
i have text followed space , cursor...if hit backspace, first moves empty space , when hit backspace again whole text deleted....
here screenshot ..
here cursor not in between 2 textview
s unlike in facebook app again causes issues in inserting text...
found solution....
add textwatcher multiautocompletetextview
private textwatcher textwather = new textwatcher() { int noofcharadded=0;int noofchardeleted=0; @override public void ontextchanged(charsequence s, int start, int before, int count) { startidx=start; } @override public void beforetextchanged(charsequence s, int start, int count,int after) { noofcharadded=after; noofchardeleted=count; } @override public void aftertextchanged(editable s) { editable buffer = s; int start = multicontenttext.getselectionstart()<0?0:multicontenttext.getselectionstart(); int end = multicontenttext.getselectionend()<0?0:multicontenttext.getselectionend(); if(noofcharadded==0 && noofchardeleted==1){ //if space deleted if (start == end && delprevtext) { imagespan link[] = buffer.getspans(start, end,imagespan.class); if (link.length > 0) { buffer.replace(buffer.getspanstart(link[0]),buffer.getspanend(link[0]),""); buffer.removespan(link[0]); } } delprevtext=true; multicontenttext.setselection(multicontenttext.gettext().length()); } else if(noofcharadded==0 && noofchardeleted>1){//if whole word deleted if(buffer.length()>0){ if(start<buffer.length()){ delprevtext=false; if(buffer.charat(start)==' '){ buffer.replace(start,start+1,""); } } } } } };
Comments
Post a Comment