49

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

4 Answers 4

113

Use XML CDATA

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

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

Sign up to request clarification or add additional context in comments.

5 Comments

No, it is not, this is far too complicated: stackoverflow.com/a/18199543/89818
Mine is stripping it off. I am using the following code: getResources().getText(R.string.codename)
This doesn't work for me. I was receiving an exception on getResources().getString() saying my R.string.mystring did not exist. I did a clean and rebuild and R would not build. I removed my <string> definition and R now builds. For some reason, the compiler does not like my <string> definition. It is exactly like yours except <b>ABC</b> is replaced with my own html.
@MarcoW.: The OP wants to know how to display the complete string including the tags and not how to format the text, hence the answer is correct.
Why do you need inner <Data> tag? I think using only CDATA is enough
39

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

2 Comments

When i use it it print data along the tag like not html formatting
@Sarwar Erfan: it prints the tag but it doesn't actually styling the string, does it?
16

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

Comments

2

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>

1 Comment

Good Answer. Just one thing: You can have apostrophes, you just need to escape them with a \ : \' I used them because I needed to store javascript in strings.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.