Android EditText : How to apply Foreground color span as you type? -
i'm trying apply font color text in edittext type text. however, inconsistent, meaning if type space, text preceding space go default black color. or if put cursor in middle of word , start typing entire word changes color , not text i'm typing. bold, italic , underline seem work though. how can guarantee text i'm typing affected regards font color?
see "size , color" comment below...
contentedit.addtextchangedlistener(new textwatcher() { public void aftertextchanged(editable s) { //add style user types if toggle button enabled togglebutton boldbutton = (togglebutton) findviewbyid(r.id.bold); togglebutton embutton = (togglebutton) findviewbyid(r.id.italic); togglebutton underlinebutton = (togglebutton) findviewbyid(r.id.underline); int position = selection.getselectionstart(contentedit.gettext()); try{ if (position < 0){ position = 0; } if (position > 0){ if (stylestart > position || position > (cursorloc + 1)){ //user changed cursor location, reset if (position - cursorloc > 1){ //user pasted text stylestart = cursorloc; } else{ stylestart = position - 1; } } if (boldbutton.ischecked()){ stylespan[] ss = s.getspans(stylestart, position, stylespan.class); (int = 0; < ss.length; i++) { if (ss[i].getstyle() == android.graphics.typeface.bold){ s.removespan(ss[i]); } } s.setspan(new stylespan(android.graphics.typeface.bold), stylestart, position, spannable.span_exclusive_exclusive); } if (embutton.ischecked()){ stylespan[] ss = s.getspans(stylestart, position, stylespan.class); (int = 0; < ss.length; i++) { if (ss[i].getstyle() == android.graphics.typeface.italic){ s.removespan(ss[i]); } } s.setspan(new stylespan(android.graphics.typeface.italic), stylestart, position, spannable.span_exclusive_exclusive); } if (underlinebutton.ischecked()){ underlinespan[] ss = s.getspans(stylestart, position, underlinespan.class); (int = 0; < ss.length; i++) { s.removespan(ss[i]); } s.setspan(new underlinespan(), stylestart, position, spannable.span_exclusive_exclusive); } //size , color////////////////////////////////////////////////////// s.setspan(new foregroundcolorspan(m_color), position, position, spannable.span_inclusive_inclusive); s.setspan(new absolutesizespan(m_cursize, true), position, position, spannable.span_inclusive_inclusive); } } catch(exception e){ //toast.maketext(m_ctx, m_ctx.gets, toast.length_long).show(); showmessage(r.string.note_warning_style,m_utils.msgtype_warning); } cursorloc = selection.getselectionstart(contentedit.gettext()); }
you can use pattern finding words , apply span words.
@override public void aftertextchanged(editable s) { spannable textspan = s; final pattern pattern = pattern.compile("\\w+"); final matcher matcher = pattern.matcher(textspan); while (matcher.find()) { start = matcher.start(); end = matcher.end(); textspan.setspan(new foregroundcolorspan(getresources().getcolor(r.color.red)), start, end, spannable.span_exclusive_exclusive); }
that's it. highlight matching words typing. hope help.
Comments
Post a Comment