2

I have a string array:

String[] words = {"spades", "hearts", " diamonds", "clubs"};

I would like to change the color of the text of all the occurrences of these words within a TextView from the regular black color to red. How would I go about doing this (if there is a way)? Thanks!

Bobby
  • 1,416
  • 1
  • 17
  • 30
  • 2
    Take a look [here](http://stackoverflow.com/questions/7221930/change-text-color-of-one-word-in-a-textview) – NamNH Mar 01 '16 at 02:14
  • 1
    Have a look at SpannableString and SpannableStringbuilder http://developer.android.com/reference/android/text/SpannableString.html – David Mar 01 '16 at 02:16
  • I have already seen this method. This would cause a great hassle since there will be multiple occurences of each word at various and unpredictable spots throughout a fairly long string of text. – Bobby Mar 01 '16 at 02:16
  • This SpannableString appears to be my best bet, I suppose. – Bobby Mar 01 '16 at 02:19

2 Answers2

6

You can easily do this by using SpannableString. Let's say that you want to colour abc in the string fooabcbar green:

String s = "fooabcfoo";
SpannableString spannable = new SpannableString(s);
spannable.setSpan(new ForegroundColorSpan(Color.GREEN), 3, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE));
view.setText(spannable)

This is essentially what Html.fromHtml() suggested by jackson95 does behind the scenes.

The last parameter to .setSpan() controls how the span changes when characters are inserted at the edge of the span. "exclusive" means that the span will not extend to cover a new character. If the TextView is not editable, then it doesn't actually matter what you put there.

You can add any number of spans to a string, so you can easily loop through the string, looking for words to highlight and simply add spans for each one.

There are plenty of other spans one can add, that controls things such as fonts and other types of decoration. You can even create your own spans that call arbitrary drawing commands, This is done by implementing the .draw() method in ReplacementSpan.

Elias Mårtenson
  • 3,820
  • 23
  • 32
1

(Below presents a method without using SpannableString because I hadn't came across that before until now. Well, that's what SO's about, learning new things :) )

What you can do is to use simple HTML tags and build the HTML string dynamically using a java method that goes through content, inserting the HTML <font color='#FF0000'> and </font> tags whenever you come across a word matching any string in words[]. Something along the line like this:

String[] words = {"spades", "hearts", " diamonds", "clubs"};
String content = "Lorem Ipsum" //the raw text to put in the TextView;
String[] splitcontent = text.split(" "));
for (int i = 0; i < array.length; i++) {
    if (/*splitcontent[i].toLowerCase(Locale.getDefault()) == any word in string array (use RegExp)*/) {
        /*insert html tag before and after word within splitcontent*/;
    }
}
String htmlstring = splitcontent.method()/*merge array entries back together with spaces*/;
myTextView.setText(Html.fromHtml(htmlstring));
jackson95
  • 136
  • 10