75

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

1

4 Answers 4

66

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
2
25

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.

1
  • This list is not complete or not up-to-date. For example <h1>, <h2> etc. are supported, too!
    – scai
    Dec 19, 2016 at 17:50
12

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

2
  • 3
    This method was deprecated in API level 24. use fromHtml(String, int) instead.
    – z3ntu
    Sep 22, 2016 at 11:12
  • @z3ntu Did you look at its source code? It calls the newer API. Plus, on older Android devices that's the only one Available anyway. Feb 9, 2018 at 13:06
5

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.