I believe there's an easier, nicer way to go about achieving this: simply create a backgroundColorSpan and add it to a SpannableString. Something along those lines:
public static SpannableString buildBackgroundColorSpan(SpannableString spannableString,
String text, String searchString, int color) {
int indexOf = text.toUpperCase().indexOf(searchString.toUpperCase());
try {
spannableString.setSpan(new BackgroundColorSpan(color), indexOf,
(indexOf + searchString.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} catch (Exception e) {
}
return spannableString;
}
Where "spannableString" is the SpannableString object created and assumed to be initialised with "text"; "searchString" represents the piece of text you wish to "highlight" in your TextView, and "color" the background colour to which the "highlighted" text should be set.
String text = "The Quick Brown Fox Jumps over the Lazy Dog";
String searchString = "brown";
int color = Color.parseColor("#FFF5F19E");
SpannableString spannableString = new SpannableString(text);
spannableString = StringUtils.buildBackgroundColorSpan(spannableString, text, searchString, color);
I think this should suffice.
Thanks