android string.xml reading html tags problem

In Android project's strings.xml file i have following html text

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="myHeadStr"><b><u>bold, underline </u></b></string>
...

</resources>

When i read this into getString(R.string.myHeadStr) it gives only text "bold, underline" it forgets the html tags and ....

how to read complete string with html tags from string.xml


Solution 1:

Use XML CDATA

<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>

getString() will be got "<b>ABC</b>"

Solution 2:

Replace < with &lt;

<string name="myHeadStr">&lt;b>&lt;u>bold, underline &lt;/u>&lt;/b></string>

Then, when retrieving:

Html.fromHtml(getResources().getString(R.string.myHeadStr));

This is the prescribed way of doing in android documentation. Read the paragraph titled: "Styling with HTML markup" in this link: http://developer.android.com/guide/topics/resources/string-resource.html

Solution 3:

Directly passing the string resource id to setText() or using Context.getText() without Html.fromHtml() works properly but passing in the the result of Context.getString() does not.

ex:

strings.xml:

<resources>
    <string name="html">This is <b>bold</b> and this is <i>italic</i>.</string>
<resources>

code in Activity.java file:

textView.setText(R.string.html); // this will properly format the text
textView.setText(getText(R.string.html)); // this will properly format the text
textView.setText(getString(R.string.html)); // this will ignore the formatting tags

Solution 4:

i've run into the same problem by trying to store a complete htmlpage in my rescources. I finaly solved the problem by changing three things:

  1. the "string" node needs to have set the "formatted" attribute to false.
  2. the stored html page needs to be wrapped in a CData node.
  3. the html page is NOT alowed to contain apostrophes!

The last one actualy was my main problem. So here is my strings.xml containing the "properly" stored html page.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="error_html" formatted="false" ><![CDATA[<html><head><link name="icon1" href="favicon.ico" rel="SHORTCUT ICON" /><title>Error</title><style>html, body {margin: 0;padding: 0;background: #3f0000;color: white;font-family: Arial;}#MainLink {position: relative;background: #7f0000;margin: 10px;text-decoration: none;border: 1px solid #9f0000;-webkit-border-radius: 10px;-moz-border-radius: 10px;border-radius: 10px;-webkit-box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);-moz-box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);box-shadow: 5px 5px 5px 0px rgba(0,0,0,0.5);}#MainLink {width: 462px;height: 220px;}#MainLink td {font-size: 20px;}#MainLink span {text-decoration: underline;font-weight: bold;font-size: 40px;}</style></head><body><table width="100%" height="100%" cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><table cellpadding="0" cellspacing="0"><tr><td colspan="2" id="MainLink" align="center"><big><big><b>Error</b></big></big></td></tr></table></td></tr></table></body></html>]]></string>
</resources>