Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 217
holgerm Avatar asked Mar 17 '12 21:03

holgerm


People also ask

What is HTML used for Android?

Android Studio is the official integrated development environment for Google's Android operating system, built on JetBrains' IntelliJ IDEA software and designed specifically for Android development. In Android, we usually need HTML files for displaying the content in WebView.

What is the use of TextView in Android?

TextView is the user interface which displays the text message on the screen to the user. It is based on the layout size, style, and color, etc. TextView is used to set and display the text according to our specifications.

Can you make a TextView clickable in Android?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

What is Spannable string in Android?

↳ android.text.SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached. For mutable text, see SpannableStringBuilder .


2 Answers

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
like image 157
Rags93 Avatar answered Sep 30 '22 12:09

Rags93


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.

like image 43
gcbound Avatar answered Sep 30 '22 13:09

gcbound