Android’s TextView widget is actually quite robust. It supports various fonts, styles, colors, etc., allowing you to have newlines, bold sections, and more (even defined from XML); however, sometimes the text you are dumping into a TextView is HTML. Fortunately, it’s easy to add HTML to a TextView:
Html.fromHtml() returns a Spanned and the TextView does the rest for you. You don’t have to worry about parsing any HTML; it is handled by XMLReader for you, which is extremely fast. Your TextView will show bold text, italicized text, links, and… wait, the links are blue and underlined but not usable? Fortunately, all you have to do is set the TextView to use the LinkMovementMethod like this:
The setMovementMethod call will allow the arrow keys, trackball, touch screen, etc. to interact with the anchor tags in your HTML. I found that the call to setLinksClickable was necessary on Android 1.5 but not 2.2.1. It’s worth noting that the setMovementMethod call (when not passed null) will actually also call these methods on your TextView:
setFocusable(true); setClickable(true); setLongClickable(true);
If you want any of those values to be false, you’ll have to call the applicable method after the setMovementMethod call.
So, what tags are supported? There isn’t an official list in the documentation, so I recommend sticking to the most common tags (br, b, i, etc.); however, the following tags have at least some handling done in the Html class:
Really though, if your HTML is complex enough to contain images, inline CSS, etc., you should be using a WebView.