Can textview have letters in different colors? [duplicate] Can textview have letters in different colors? [duplicate] android android

Can textview have letters in different colors? [duplicate]


Yes, you can have different colors in different places of the text if you are using SpannableString. Example:

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);  textView.setText(text, BufferType.SPANNABLE);

There's a more complete example here.

Javadoc for SpannableString


Ah I found it use below code

myTextView.setText(Html.fromHtml(html text having 1 in red 2 in green and so on));

I dont know web so you better consult someone who can write html for you :P


You can prints lines with multiple colors without html like this:)

TextView TV = (TextView)findViewById(R.id.mytextview01);  Spannable WordtoSpan = new SpannableString("Your message");        WordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 0, WordtoSpan .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);TV.setText(WordtoSpan);