HelloCW
HelloCW

Reputation: 2255

Why can't the font color be displayed when I read a string from resource?

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>

Upvotes: 0

Views: 1080

Answers (1)

Antrromet
Antrromet

Reputation: 15414

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.

Upvotes: 3

Related Questions