Android TextView And HTML Links

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:


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:

  • a
  • b
  • big
  • blockquote
  • br
  • cite
  • dfn
  • div
  • em
  • font
  • h1-h6
  • i
  • img
  • p
  • small
  • strong
  • sup
  • sup
  • tt
  • u

Really though, if your HTML is complex enough to contain images, inline CSS, etc., you should be using a WebView.

About Ian G. Clifton

Ian currently works as a Senior Software Developer at Vulcan, Inc. in Seattle, WA. Previously, he has worked as a Director of User Experience, Android developer, web developer, and even a Satellite, Wideband, and Telemetry Systems Journeyman in the United States Air Force. He created the Essentials of Android Application Development LiveLessons video series and wrote Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps. He spends his off time on photography, drawing, developing, and doing technical review for other Android developers, and woodworking. You can follow his posts on this blog or his ramblings on Twitter.
This entry was posted in Code Samples and tagged , , , . Bookmark the permalink.

6 Responses to Android TextView And HTML Links

  1. Shane says:

    Does the style attribute works?

    I have a text that was created using Html.fromHtml(htmlString), but once its rendered, it has an underline.

    Any idea on how to remove the underline?


    • I’d first check whether it’s underlined because of a link or a pair of “u” tags (or some other reason). You may have to modify the Spanned that is returned from the Html.fromHtml() call (or modify the HTML being passed to that beforehand).

  2. Rich says:

    I have two links on my login screen (one for create new account, the other for forgot password). Every once in a while, the first link loses it’s “link style” (blue text with underline). Have you ever run into this issue? I am coding to Android 2.3.3 and up, so I don’t think I need textView.setLinksClickable(true).

    • I have never run into that issue before. Is there some trigger that causes it to lose its style such as after it or the other link is tapped?

      • Rich says:

        No, this happens sporadically, and it shows up like this before either link is clicked…when the user first opens the application. On the XML side, the TextView has android:clickable set to true. In the code-behind, we are setting the text of the TextView (after retrieving it from strings.xml), then setting the setMovementMethod property to LinkMovementMethod.getInstance().

        • That sounds like it should work fine; I’ve done something similar in the past. I’m wondering if something else is somehow interfering. Perhaps you can make a minimal test case in a fresh project. If nothing else, you can manually create ClickableSpans for the links.

Comments are closed.