2

Possible Duplicate:
Different colors in TextView defined in XML

This is probably a really stupid question but I couldn't figure this out. What I want to do is change the color of the text within the string, like so: "Hi, how are" +colorRed " you?"

^ trying to change the word "you" to be the color red.

Community
  • 1
  • 1
Adariel Lzinski
  • 1,031
  • 2
  • 19
  • 43

3 Answers3

4

Easiest way I know is to just use html.

String color = "Hi, how are <font color='#EE0000'>you</font>";
**YOUR_TEXTVIEW**.setText(Html.fromHtml(color));
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Arvind Kanjariya
  • 2,089
  • 1
  • 18
  • 23
2
String s = "Hi, how are <font color='red'>you</font>?";
textView.setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);
crbin1
  • 2,219
  • 3
  • 22
  • 29
0

I think the best (and less time-consuming) way is - use a Spannable. For example:

final String text = "Hi, how are ";
final Spannable spanYou = new SpannableString("you");
spanYou.setSpan(new ForegroundColorSpan(Color.RED), 0, spanYou.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(text);
textView.append(spanYou);

Html.fromHtml() at first parse your text as html, and use Spannable after.

nfirex
  • 1,523
  • 18
  • 24