android - How to change color of words with hashtags -
i need able display text words starting # in different color , should clickable. how can this?
this should trick
private void settags(textview ptextview, string ptagstring) { spannablestring string = new spannablestring(ptagstring); int start = -1; (int = 0; < ptagstring.length(); i++) { if (ptagstring.charat(i) == '#') { start = i; } else if (ptagstring.charat(i) == ' ' || (i == ptagstring.length() - 1 && start != -1)) { if (start != -1) { if (i == ptagstring.length() - 1) { i++; // case if hash last word , there no // space after word } final string tag = ptagstring.substring(start, i); string.setspan(new clickablespan() { @override public void onclick(view widget) { log.d("hash", string.format("clicked %s!", tag)); } @override public void updatedrawstate(textpaint ds) { // link color ds.setcolor(color.parsecolor("#33b5e5")); ds.setunderlinetext(false); } }, start, i, spannable.span_exclusive_exclusive); start = -1; } } } ptextview.setmovementmethod(linkmovementmethod.getinstance()); ptextview.settext(string); }
Comments
Post a Comment