android - TextView with different colors -
i have problem code. trying make app checks if user writes sentence in right way. if doesn't, see words wrote wrong in red, otherwise see whole sentence in green.
check_b.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { final string[] dan_res = dan.split(" "); string[] text_res = check_text.gettext().tostring().split(" "); (int = 0; i<text_res.length; i++) { if (text_res[i] != dan_res[i]) { spannablestring text = new spannablestring(text_res[i]); // make "lorem" (characters 0 5) red text.setspan(new foregroundcolorspan(color.red), 0, i, 0); dan_view.append(text_res[i]); } else { dan_view.settextcolor(color.green); } } } });
perhaps easiest way use html:
textview.settext(html.fromhtml("<font color='red'>this red.</font><font color='green'>and green.</font>"));
Comments
Post a Comment