Android string.xml problema de lectura de etiquetas html

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

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


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


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

preguntado el 09 de enero de 11 a las 05:01

4 Respuestas


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

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

contestado el 31 de mayo de 12 a las 15:05

No, it is not, this is far too complicated: - graznar

Mine is stripping it off. I am using the following code: getResources().getText(R.string.codename) - Si8

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. - Andrés

@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. - Julian Sievers

Why do you need inner <Data> tag? I think using only CDATA is enough - Vadim Kotov

Replace < with &lt;

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

Then, when retrieving:


This is the prescribed way of doing in android documentation. Read the paragraph titled: "Styling with HTML markup" in this link:

Respondido el 09 de enero de 11 a las 08:01

When i use it it print data along the tag like not html formatting - d-man

@Sarwar Erfan: it prints the tag but it doesn't actually styling the string, does it? - Duc Tran

Directly passing the string resource id to setText() o utilizando Context.getText() sin Html.fromHtml() works properly but passing in the the result of Context.getString() no.



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

codificar en archivo:

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

Respondido 20 ago 18, 12:08

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 NO 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"?>
    <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>

respondido 07 nov., 14:13

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. - danielnovais92

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.