Which HTML tags are supported by Android TextView?

Android's TextView class can display formatted text via HTML.fromHtml() as explained for example here: HTML tags in string for TextView

The TextView class can only deal with a small subset of HTML, but I do not know which tags and attributes are supported and which are not. The summary given here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html does not seem to be correct. E.g. <div align="..."> does NOT work for me using Android 2.2


Solution 1:

Looked it up for everyone searching for it.

Date: July 2017

Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java

Html.fromHtml supports:

  • p
  • ul
  • li
  • div
  • span
  • strong
  • b
  • em
  • cite
  • dfn
  • i
  • big
  • small
  • font
  • blockquote
  • tt
  • a
  • u
  • del
  • s
  • strike
  • sup
  • sub
  • h1
  • h2
  • h3
  • h4
  • h5
  • h6
  • img
  • br

Solution 2:

I noticed that this article:

https://web.archive.org/web/20171118200650/http://daniel-codes.blogspot.com/2011/04/html-in-textviews.html

lists <div> as being supported by Html.fromHtml(), but it doesn't show support for the "align" attribute.

(Other supported attributes are shown for tags on that page.)

The author says he constructed the reference by looking at code in the git repositories for Android.

Edit: Over time, it appears the list of supported tags has changed. See this later post for example: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ .

Based on both those articles, I'd suggest that examining the source code seems to be the most reliable way to get the recent information.

Solution 3:

The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.

//in string.xml file

<string name="welcome_text"><![CDATA[<b>Welcome,</b> to the forthetyroprogrammers blog Logged in as:]]> %1$s.</string>

Java code

String welcomStr=String.format(getString(R.string.welcome_text),username);
tvWelcomeUser.setText(Html.fromHtml(welcomStr));

CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.

Output:

Welcome, to your favorite music app store. Logged in as: username

Solution 4:

Since it is constantly being updated, the best way to track which HTML tags are supported in Android is to check the source code of Html.java