HTML in string resource?

You can also surround your html in a CDATA block as well and getString() will return your actual HTML. Like such:

<string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>

Now when you perform a getString(R.string.foo) the string will be HTML. If you need to render the HTML (with the link as shown) via a clickable TextView you'd need to perform a Html.fromHtml(...) call to get the spannable text.


It seems getString() does just that -- gets a string. To use this, you have to use getText() (and no more Html.fromHtml()), i.e.:

mTextView.setText(getText(R.string.my_styled_text));

However, it seems the android:text property does just the same thing, and the following is equivalent:

<TextView android:text="@string/my_styled_text" />

And in strings.xml:

<string name="my_styled_text">Hello, <b>World</b>!</string>

Tags:

Android