3

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 spanis 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.

Shahin Ghasemi
  • 1,600
  • 1
  • 20
  • 38

2 Answers2

2

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);
  }
 }
Gil Vegliach
  • 3,542
  • 2
  • 25
  • 37
  • Thanks it works but not perfectly , for example when my Span is "Name , Name" it color the last Name not all of the specified words(in this case Name) – Shahin Ghasemi Aug 27 '17 at 08:00
  • 1
    hey i got the problem , please change `span.setSpan(blue, i, i + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);` to `span.setSpan(new ForegroundColorSpan(Color.BLUE), i, i + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);` then i'll accept the answer ;) – Shahin Ghasemi Aug 27 '17 at 08:38
  • @shahingh: Cool, thanks! Changed. A previous version was like that already but then I thought it was more efficient to extract the variable. Good to know it doesn't work :) – Gil Vegliach Aug 27 '17 at 09:32
1

This is how making multiple color and word :

    SpannableStringBuilder builder = new SpannableStringBuilder(); 
String red = "this is red"; 
SpannableString redSpannable= new SpannableString(red); 
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0); 
builder.append(redSpannable); 
String white = "this is white"; 
SpannableString whiteSpannable= new SpannableString(white); 
whiteSpannable.setSpan(new ForegroundColorSpan(Color.WHITE), 0, white.length(), 0); 
builder.append(whiteSpannable); 
String blue = "this is blue"; 
SpannableString blueSpannable = new SpannableString(blue); 
blueSpannable.setSpan(new ForegroundColorSpan(Color.BLUE), 0, blue.length(), 0); builder.append(blueSpannable); mTextView.setText(builder, BufferType.SPANNABLE);

For finding word indexes do as follow :

int first = str.indexOf("hi"); 
int next = str.indexOf("hi", first+1);

read api

Do this to get all indexes and color them

Alireza Sharifi
  • 1,127
  • 1
  • 9
  • 18
  • thank you , i set my span once , and i want a way to check the specified words after setting , just like the way **Gil vegliach** – Shahin Ghasemi Aug 27 '17 at 08:03