0

I hope to set single TextView with two different colored text, I have the read the document

Single TextView with multiple colored text

Code A works well!

Code B get string from resource file, but the color of font can not be displayed, why?

Code A

 TextView my=(TextView) findViewById(R.id.tVTitleOld);
 String text = "<font color='#cc0029'>Hello</font> the <font color='#ffcc00'>world</font>";
 my.setText(Html.fromHtml(text));

Code B

 TextView my=(TextView) findViewById(R.id.tVTitleOld);
 String text = getResources().getString(R.string.TitleOld);
  my.setText(Html.fromHtml(text));


<string name="TitleOld"><font color='#cc0029'>Hello</font> the new <font color='#ffcc00'>world</font></string>
Community
  • 1
  • 1
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

3

HTML code in Strings.xml should be stored in CDATA using something like

<![CDATA[html source code]]>

So, in short your code would be

<string name="TitleOld"><![CDATA[<font color=\'#cc0029\'>Hello</font> the new <font color=\'#ffcc00\'>world</font>]]></string>

This is because < is stored in the xml file as &lt;, > as &gt; and so on. CDATA maintance the actual text whatever you need to display.

Antrromet
  • 15,294
  • 10
  • 60
  • 75