12

I need my textview to have different colored texts. Also I need to do this from xml code, non from java code. Is there anyone who knows some way for doing this? Thanks

e.g. I have sentence "This is red". I need words to be green, and word red to be red.

Hayk Nahapetyan
  • 4,452
  • 8
  • 42
  • 61

5 Answers5

16

There are three ways to change the color of some text inside a textview.

  1. through strings.xml file in (res>values), using the tag (<![CDATA[<p>This is green <font color='hexvalue of red'>and this is red</font>.</p> ]]>) and then declaring the textview in java code as myTextView.setText(Html.fromHtml(getString(R.string.myText));

  2. through java code, using the HTML tag String text = "<font color='hexvalue of green'>This is green</font> <font color='hexvalue of red'>and this is red</font>."; myTextView.setText(Html.fromHtml((text));

  3. through Spannable text using java code.

    Spannable span = new SpannableString("My String");

    span.setSpan(new ForegroundColorSpan(Color.RED), start_position, end_position,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    myTextView.setText(span);

If there are other ways to do it then I'm not aware of them. Hope this helps

Riz Khan
  • 304
  • 2
  • 6
15

Refer your text to string.xml and using html font tag , by using that way you can change each letter color also .

just add this in java for that string:

  TextView tv=(TextView)findViewById(R.id.tv);

  tv.setText(Html.fromHtml(getString(R.string.any_text)));

and

In string.xml:

 <string name="any_text">
 <![CDATA[ <b><font color=#ff0000>write</b> your <b><font color=#0000ff>text</b> here .

]]> 
  </string>

hope help you

androidqq6
  • 1,526
  • 2
  • 22
  • 47
1

If you want to give text color in strings.xml then check out the below code:

<string name="by_continuing_i_confirm_that_i_have_nread_the_privacy_policy">
<font fgcolor='#ffffff' >By continuing, I confirm that I have \nread the</font> <font fgcolor='#2DBBDD' >Privacy Policy</font>
kishan verma
  • 984
  • 15
  • 17
0

In Java class define TextView like this:

TextView tv = (TextView) findViewById(R.id.text1);
String text = "<font color=#cc0029>write any thing here</font> "+
              "<font color=#ffcc00>write any thing here 2</font>";
tv.setText(Html.fromHtml(text));
marklark
  • 860
  • 1
  • 8
  • 18
Hurab
  • 169
  • 1
  • 4
-5
<TextView
    android:id="@+id/yourUniqueTextViewID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@color/RED" />

Where "RED" is a named constant you have to define under res/values/ in an xml file. Typically i create "colors.xml".

Or see this for a good set of predefined colors: Web colors in an Android color xml resource file

Community
  • 1
  • 1
cosmiczilch
  • 43
  • 2
  • 7
  • 1
    Thanks for reply dear friend ,but I meant this. e.g. I have sentence "This is red". I need words to be green, and word to be red. – Hayk Nahapetyan Jul 09 '13 at 22:40