Search code examples
androidhtmltextcolor

android can't set special color using html


in the string file:

<string name="test1">AAAA<font color="#ff0000">BBBB</font></string>
<string name="test2">AAAA<font color="red">BBBB</font></string>

in the layout file,I set a TextView like this:

android:text="@string/test1"

but it did't work!! the "BBBB" not turn to "#ff0000".

when I set the TextView like:

android:text="@string/test2"

it works.the "BBBB" turn to red.

I want to know how can I set the text with the color that I want; not just red blue green...


Solution

  • Use SpannableString for ex:

    SpannableString ss = new SpannableString("hi #xyz how are you.");
    ss.setSpan(new ForegroundColorSpan(Color.RED), 3, 8, 0);