I want to know how can i detect some specified words in SpannableStringBuilder
.
My purpose is to change color of this words.
Say i have SpannableStringBuilder span
and the list included my Words like "Name" , "Family" , "Location" etc.
In this stage i want to check if my span
is including these words then change color of.
For example I want something like :
if(span.contain("Name")) // Change Color of the word "Name" every where in this span
if(span.contain("Family")) // Change Color of the word "Family" every where in this span
and so ...
Is there any method to check ? any code example will be appreciated.
There is no search method in SpannableStringBuilder
but you can use indexOf()
after you convert it to a String
:
Set<String> words = new HashSet<String>() {{
add("Name"); add("Family"); add("Location");
}};
String s = span.toString();
for (String word : words) {
int len = word.length();
// Here you might want to add a check for an empty word
for (int i = 0; (i = s.indexOf(word, i)) >= 0; i += len) {
span.setSpan(new ForegroundColorSpan(Color.BLUE), i, i + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}