3

In my strings.xml file I have defined the following:

<string name="mystring"><b>Bold text</b>Non-bold text</string>

It should work, as it's specified here. But actually only bold text is displayed, and the other part of text is gone.

Yury Pogrebnyak
  • 4,093
  • 9
  • 45
  • 78

1 Answers1

6

taken from this SO thread

use this

textView.setText(Html.fromHtml(someText));

or from xml by this way

You CAN include raw HTML in strings.xml, as long as you wrap it in

<![CDATA[ ...raw html... ]]>

Example:

<string name="nice_html">
<![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
]]>
</string>

Then, in your code:

TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html)));

you can also backslash-escape apostrophes/single-quotes inside the CDATA block, so you can have things like <b>can\'t</b> instead of the infinitely-uglier <b>can&apos;t</b>

Community
  • 1
  • 1
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • But what if I need to use strings.xml for different localisations? – Yury Pogrebnyak May 28 '12 at 13:37
  • Thanks, it works. And the last question - can I somehow set html text in layout.xml file, or only in code? – Yury Pogrebnyak May 28 '12 at 13:52
  • 1
    i think you can't if you set your html text in your android:text attribute you will get your html tag printed too, the trick is to use the class Html and then use the method from Html and all that with code!! – K_Anas May 28 '12 at 13:59