Set TextView text from html-formatted string resource in XML

Just in case anybody finds this, there’s a nicer alternative that’s not documented (I tripped over it after searching for hours, and finally found it in the bug list for the Android SDK itself). You CAN include raw HTML in strings.xml, as long as you wrap it in

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

Edge Cases:

  • Characters like apostrophe (‘), double-quote (“), and ampersand (&) only need to be escaped if you want them to appear in the rendered text AS themselves, but they COULD be plausibly interpreted as HTML.
    • ' and " can be represented as\' and \", or &apos; and &quot;.
    • < and > always need to be escaped as &lt; and &gt; if you literally want them to render as ‘<‘ and ‘>’ in the text.
    • Ampersand (&) is a little more complicated.
      • Ampersand followed by whitespace renders as ampersand.
      • Ampersand followed by one or more characters that don’t form a valid HTML entity code render as Ampersand followed by those characters. So… &qqq; renders as &qqq;, but &lt;1 renders as <1.

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 from the same \'string\'.</p>
<p>To be clear, 0 &lt; 1, & 10 &gt; 1<p>
]]>
</string>

Then, in your code:

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

IMHO, this is several orders of magnitude nicer to work with 🙂


August 2021 update: My original answer used Html.fromHtml(String), which was deprecated in API 24. The alternative fromHtml(String,int) form is suggested as its replacement.

FROM_HTML_MODE_LEGACY is likely to work… but one of the other flags might be a better choice for what you want to do.

On a final note, if you’d prefer to render Android Spanned text suitable for use in a TextView using Markdown syntax instead of HTML, there are now multiple thirdparty libraries to make it easy including https://noties.io/Markwon.

Leave a Comment