android - Replace a tapped word with custom suggested words in edit text -
i want tap on word in edit text. when word clicked, should show set of custom suggested words in popup window former word can replaced suggested word. please me how this. the suggestions should not shown while type word.
i have implemented required feature. not elegant solution. so, if can make code more efficient, welcome.
string meow = "i dont asu @ all"; // string supposed set in edit text final string[] tokens = meow.split("\\s+"); tokenlist = new arraylist<string>(arrays.aslist(tokens)); //create custom dictionary file used in suggested words dictionary = new hashmap<string, list<string>>(); dictionary.put("i", arrays.aslist("buenos aires", "córdoba", "la plata")); dictionary.put("dont", arrays.aslist("mumbai", "delhi", "gwalior")); dictionary.put("like", arrays.aslist("phoenix", "tucson", "flagstaff")); dictionary.put("asu", arrays.aslist("uofa", "ucsb", "ucla")); dictionary.put("at", arrays.aslist("sameeran", "nikhil", "aniket")); dictionary.put("all", arrays.aslist("asas", "dfdf", "tytyty")); dictionary.put("gwalior", arrays.aslist("hardwork", "maketh", "luck")); if (meow != null || meow.length() != 0 ) { _field.setmovementmethod(linkmovementmethod.getinstance()); //set text edittext using addclickablepart() function _field.settext(addclickablepart(meow, tokenlist), edittext.buffertype.spannable); } // function regenerates clickable span after word replaced public void tokengenerator(){ string edittext_sentence = _field.gettext().tostring(); _field.setmovementmethod(linkmovementmethod.getinstance()); string[] intermediate_tokens = edittext_sentence.split("\\s+"); arraylist<string> intermediate_tokenlist = new arraylist<string>(arrays.aslist(intermediate_tokens)); _field.settext(addclickablepart(edittext_sentence, intermediate_tokenlist), edittext.buffertype.spannable); //addclickable function defined here public spannablestringbuilder addclickablepart(string str, arraylist<string> clickablewords) { spannablestringbuilder ssb = new spannablestringbuilder(str); (final string clickableword : clickablewords) { int idx1 = str.indexof(clickableword); int idx2 = 0; while (idx1 != -1) { idx2 = idx1 + clickableword.length(); final string clickstring = str.substring(idx1, idx2); // ssb.setspan(new touchablespan(clickstring), idx1, idx2, 0); ssb.setspan(new touchablespan(clickstring) { @override public void onclick(view view) { final spanned s = _field.gettext(); final int start = s.getspanstart(this); final int end = s.getspanend(this); word = s.subsequence(start, end).tostring(); final popupwindow popupwindow = new popupwindow(simpleandroidocractivity.this); final arraylist<string> bua = new arraylist<string>(dictionary.get(clickableword)); arrayadapter<string> adapter = new arrayadapter<string>(getapplicationcontext(), android.r.layout.simple_dropdown_item_1line, bua); final listview listview = new listview(simpleandroidocractivity.this); final edittext addword = new edittext(simpleandroidocractivity.this); addword.requestfocusfromtouch(); addword.setinputtype(inputtype.type_text_flag_ime_multi_line); addword.setimeoptions(editorinfo.ime_action_done); addword.setoneditoractionlistener(new textview.oneditoractionlistener() { //press done button replace typed word @override public boolean oneditoraction(textview v, int actionid, keyevent event) { if (actionid == editorinfo.ime_action_done) { stringbuffer sb = new stringbuffer(s.tostring()); sb.replace(start, end, addword.gettext().tostring()); _field.settext(sb.tostring()); tokengenerator(); if (popupwindow != null) { popupwindow.dismiss(); } } return false; } }); addword.sethint("add word"); listview.setadapter(adapter); listview.addfooterview(addword,null, true); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long id) { int itemposition = position; string itemvalue = (string) listview.getitematposition(position); stringbuffer sb = new stringbuffer(s.tostring()); sb.replace(start, end, itemvalue); _field.settext(sb.tostring()); tokengenerator(); inputmethodmanager imm = (inputmethodmanager)getsystemservice(context.input_method_service); imm.hidesoftinputfromwindow(view.getwindowtoken(), 0); if (popupwindow != null) { popupwindow.dismiss(); } } }); // other visual settings popup window popupwindow.setfocusable(true); popupwindow.setoutsidetouchable(true); popupwindow.setwidth(400); popupwindow.setheight(windowmanager.layoutparams.wrap_content); popupwindow.setcontentview(listview); popupwindow.setbackgrounddrawable(new colordrawable(color.rgb(229,197,175))); int pos = _field.getselectionstart(); layout layout = _field.getlayout(); int line = layout.getlineforoffset(pos); int baseline = layout.getlinebaseline(line); int ascent = layout.getlineascent(line); int x = (int)layout.getprimaryhorizontal(pos); int y = baseline + ascent; popupwindow.showatlocation(view, gravity.left,x,y + 30); popupwindow.showasdropdown(view, 0, 0); // show popup dropdown list } }, idx1, idx2, 0); idx1 = str.indexof(clickableword, idx2); } } return ssb; }
the following definition touchablespan class extends clickablespan class
public abstract class touchablespan extends clickablespan { string clicked; string word; public touchablespan(string string) { super(); clicked = string; } abstract public void onclick(view view); @override public void updatedrawstate(textpaint ds) { super.updatedrawstate(ds); ds.setunderlinetext(false); } }
it this:
Comments
Post a Comment