Ziem My personal blog

Html class - basic usage

From time to time, there is a need to display some styled text in an Android application. You can always use Spans to achieve your goals, but if you know HTML, you could consider using Html class.

The Html class with ImageGetter and TagHandler interfaces is used to convert HTML strings into styled text. At the time of writing Html class contains four static methods:

static String escapeHtml(CharSequence text);
static Spanned fromHtml(String source);
static Spanned fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler);
static String toHtml(Spanned text);

The first method allows us to get an HTML-escaped representation of given plain text. The fourth we can use when we want to get an HTML representation of the provided Spanned text. fromHtml methods return styled text (Spanned) from provided HTML string. You may wonder, how does it work? As you can find at https://grepcode.com Html class parses HTML strings using TagSoup which is a SAX-compliant parser written in Java.

In this post, I will focus on the second method because, in my opinion, it is used most of the time. Let’s assume that we would like to display the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ut magna eget erat fermentum dignissim. Aenean interdum ultrices sapien1. Vestibulum feugiat ultrices neque, vel pretium massa. Etiam tempor luctus dui eget aliquam. In nec metus sit amet orci facilisis pretium.

Creating Spans manually can be cumbersome so let’s use a fromHtml method.

First of all, we have to create an HTML representation of our text:

<b>Lorem ipsum dolor</b> sit <u>amet</u>, consectetur <font color="green">adipiscing</font> elit. <i>Cras ut magna eget erat fermentum dignissim.</i> Aenean interdum ultrices sapien<sup>1</sup>. <font color="red">Vestibulum feugiat ultrices neque, vel pretium massa. Etiam tempor luctus dui eget aliquam.</font> In nec metus sit amet orci facilisis pretium.

next, we put it into a string.xml file:

<string name="lorem"><![CDATA[<b>Lorem ipsum dolor</b> sit <u>amet</u>, consectetur <font color="green">adipiscing</font> elit. <i>Cras ut magna eget erat fermentum dignissim.</i> Aenean interdum ultrices sapien<sup>1</sup>. <font color="red">Vestibulum feugiat ultrices neque, vel pretium massa. Etiam tempor luctus dui eget aliquam.</font> In nec metus sit amet orci facilisis pretium.]]></string>

in the *Activity.java file we use the Html class:

TextView textView = ...;
String string = getString(R.string.lorem);
Spanned spanned = Html.fromHtml(string);
textView.setText(spanned);

and voilà:

Screenshot of the result

If you would like to know what other methods return, I’m attaching snippets.

escapedHtml method returns:

&lt;b&gt;Lorem ipsum dolor&lt;/b&gt; sit &lt;u&gt;amet&lt;/u&gt;, consectetur &lt;font color=green&gt;adipiscing&lt;/font&gt; elit. &lt;i&gt;Cras ut magna eget erat fermentum dignissim.&lt;/i&gt; Aenean interdum ultrices sapien&lt;sup&gt;1&lt;/sup&gt;. &lt;font color=red&gt;Vestibulum feugiat ultrices neque, vel pretium massa. Etiam tempor luctus dui eget aliquam.&lt;/font&gt; In nec metus sit amet orci facilisis pretium.

and toHtml method returns:

<p dir="ltr"><b>Lorem ipsum dolor</b> sit <u>amet</u>, consectetur <font color ="#00ff00">adipiscing</font> elit. <i>Cras ut magna eget erat fermentum dignissim.</i> Aenean interdum ultrices sapien<sup>1</sup>. <font color ="#ff0000">Vestibulum feugiat ultrices neque, vel pretium massa. Etiam tempor luctus dui eget aliquam.</font> In nec metus sit amet orci facilisis pretium.</p>

Be aware that the Html class doesn’t support all HTML tags. Unfortunately, the documentation doesn’t say which are supported: Issue 8640: Documentation which tags are supported by Html.fromHtml(). Thanks to Mark Murphy, you can find a list of supported tags in Android 2.1 on his blog: HTML Tags Supported By TextView. Besides, using GrepCode you can always check it by yourself.

fromHtml method parses provided string so I advise not to invoke it on the UI thread when displaying longer text. Additionally, you should consider caching returned Spanned object (e.g. in lists).

In my next posts, I will explain how to use images and handle unsupported tags with the Html class.