I have a string, and I can detect if it has an #
inside it.
if(title.contains("#")){
SpannableString WordtoSpan = new SpannableString(title);
int idx = title.indexOf("#");
if (idx >= 0) {
int wordEnd = title.indexOf(" ", idx);
if (wordEnd < 0) {
wordEnd = title.length();
}
WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED),
idx,
wordEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
holder.txtTitle.setText(WordtoSpan);
} else {
holder.txtTitle.setText(title);
}
So now, if the string has an #
it will color it till the end of the word after it with red. This works perfectly. But the problem is, when a string has more than one #
it will color only the first #
with it word, not the next or the third etc ..
ex. Now: Notice it ill color only burger with red
I love chicken #burger , cuz they are #delicious.
I want: Notice bother burger and delicious are colored.
I love chicken #burger , cuz they are #delicious .