Is it possible to have a textview
to have different color for every word? Or even every letter? I tried extending textview
and creating it but however I thought of the problem is, how would I draw all the the text out at the same time with different colors?
Asked
Active
Viewed 1.4k times
15
-
Yes, with `Html.fromHtml()` See http://stackoverflow.com/q/1529068/871102 – Xavi Gil Jul 13 '12 at 23:24
-
Thanks Xavi. Html.fromHtml() seems to work in my case. Never tried spannable so ima give that a try first before html. – wtsang02 Jul 13 '12 at 23:33
3 Answers
32
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(
new ForegroundColorSpan(Color.BLUE),
wordStart,
wordEnd,
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE
);
myTextView.setText(str);
EDIT: To make all "Java" green
final Pattern p = Pattern.compile("Java");
final Matcher matcher = p.matcher(text);
final SpannableStringBuilder spannable = new SpannableStringBuilder(text);
final ForegroundColorSpan span = new ForegroundColorSpan(Color.GREEN);
while (matcher.find()) {
spannable.setSpan(
span, matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
myTextView.setText(spannable);
-
How would i set span if I wanted all the "Java" in the textview to be green? – wtsang02 Jul 13 '12 at 23:36
-
-
One more follow up question. Am i able to put "spannable" into another loop? and set another string to a different color? – wtsang02 Jul 14 '12 at 00:08
-
1
-
21
The SpannableString
class allows you to easily format certain pieces (spans) of a string one way and other pieces another by applying extensions of CharacterStyle (i.e. ForegroundColorSpan
) via the setSpan
method.
You can try This:
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
richTextView = (TextView)findViewById(R.id.rich_text);
// this is the text we'll be operating on
SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
// make "Lorem" (characters 0 to 5) red
text.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);
// make "ipsum" (characters 6 to 11) one and a half time bigger than the textbox
text.setSpan(new RelativeSizeSpan(1.5f), 6, 11, 0);
// make "dolor" (characters 12 to 17) display a toast message when touched
final Context context = this;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Toast.makeText(context, "dolor", Toast.LENGTH_LONG).show();
}
};
text.setSpan(clickableSpan, 12, 17, 0);
// make "sit" (characters 18 to 21) struck through
text.setSpan(new StrikethroughSpan(), 18, 21, 0);
// make "amet" (characters 22 to 26) twice as big, green and a link to this site.
// it's important to set the color after the URLSpan or the standard
// link color will override it.
text.setSpan(new RelativeSizeSpan(2f), 22, 26, 0);
text.setSpan(new URLSpan("http://www.chrisumbel.com"), 22, 26, 0);
text.setSpan(new ForegroundColorSpan(Color.GREEN), 22, 26, 0);
// make our ClickableSpans and URLSpans work
richTextView.setMovementMethod(LinkMovementMethod.getInstance());
// shove our styled text into the TextView
richTextView.setText(text, BufferType.SPANNABLE);
}
The result will look like this:
For more detail see Chris Umbel blog.

K_Anas
- 31,226
- 9
- 68
- 81
4
Yes, you can do this with Spannable and SpannableStringBuilder. See Is there any example about Spanned and Spannable text for one example.
For the various ways to format text (background color, foreground color, clickable, etc.), see CharacterStyle.