I have a string, in which i have to find a word being searched from end user and make it bold to highlight.
Example:
String : Address must have a format. Address can be of multiple line.
Required Text: Address must have a format. Address can be of multiple line.
After going through different Approaches. I have written the following method which will span the searched Text in string.
If you want to span the same text multiple time they can use this method as well.
public CharSequence highlightTextString(String completeText, String searchText) {
String temp = completeText.toLowerCase();
SpannableStringBuilder highlightText = new SpannableStringBuilder(completeText);
Pattern pattern = Pattern.compile(searchText.toLowerCase());
Matcher matcher = pattern.matcher(temp);
while (matcher.find()) {
StyleSpan styleSpan = new StyleSpan(android.graphics.Typeface.BOLD);
highlightText.setSpan(styleSpan, matcher.start(), matcher.end(), 0);
}
return highlightText;
}