Which HTML tags are supported by Android TextView?
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
Which HTML tags are supported by the Android HTML class and how do I use HtmlCompat?
HtmlCompat
Let's look at HtmlCompat first. One reason to use a support library class (now AndroidX) is to "support a recent platform feature on devices that are running earlier versions of the platform." From looking at the source code for HtmlCompat, it is clear that the backward compatibility that it offers is to allow calls to HtmlCompat#fromHtml() (String source,
int flags).
[HtmlCompat#fromHtml(String, int)] invokes Html#fromHtml(String, int) on API 24 and newer, otherwise flags are ignored and Html#fromHtml(String) is used.
The same is true for HtmlCompat#toHtml(Spanned text,
int options).
So, HtmlCompat does not provide support for newly supported tags on API versions below API 24. In fact, there is no difference in the tags supported by HtmlCompat and the platform version of Html for any API level.
HTML Tags Supported by Html/HtmlCompat
(slightly out of date)
Update: See here for a more up-to-date document covering the following. I have also added information regarding native Android support of HTML tags. (December 2021)
The Android Html and HtmlCompat classes support the following HTML tags starting with API 24. The following is based upon an examination of the Html class found in API 29.
<a> Supports the href tag.
<b>
<big>
<blockquote> 1
<br>
<cite>
<del>
<dfn>
<div>>1
<em>
<font> Supports the color and face properties.
<h1> … <h6>1
<i>
<img> Supports the src tag with Html#ImageGetter.
<li>>1 2
<p>1 2
<s>
<small>
<span>2
<strong>
<strike>
<sub>
<super>
<tt>
<u>
<ul>1
Other tags can be supported with Html#TagHandler.
1 The element supports the text-align style property. The supported values for text-align are: start, center and end. (justify is not supported.)
2 The tag supports the color, background[-color] and text-decoration properties. The only supported value for text-decoration is line-through. See below for details on color support.
3 face can be any typeface name supported by the TypefaceSpan class.
Html#fromHtml() Flags
Values for the flags argument of Html#fromHtml() are:
FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE
FROM_HTML_SEPARATOR_LINE_BREAK_DIV
FROM_HTML_SEPARATOR_LINE_BREAK_HEADING
FROM_HTML_SEPARATOR_LINE_BREAK_LIST
FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH
Each of the preceding flags specifies that the HTML processor should add a single newline after each named block-level element. If the flag is not set then the processor adds two newlines which is the legacy behavior.
For example: Setting FROM_HTML_SEPARATOR_LINE_BREAK_HEADING will add one newline after a heading (<h1>, <h2>, etc.)
FROM_HTML_MODE_LEGACY: If this flag is set, then two newlines will be added after each block-level element. Setting this flag is the same as passing zero.
FROM_HTML_MODE_COMPACT: Use of this flag is the same as specifying all of the line break flags which will remove all extra newlines from block-level elements. (Only one newline will be added.)
FROM_HTML_OPTION_USE_CSS_COLORS: For named colors, use the CSS numeric values instead of the values defined by the Android Color class.
For instance, if "darkgray" is specified as the color and this flag is set then the color value will be the CSS value for “darkgray” (0xFFA9A9A9) instead of the value for “darkgray” defined in the Color class (0xFF444444). If this flag is not set then the value will be the value from the Color class.
CSS Colors
Colors defined in the Color class:
aqua, black, blue ,cyan, darkgray, darkgrey, fuchsia, gray, green, grey, lightgray, lightgrey, lime, magenta, maroon, navy, olive, purple, red, silver, teal, white, yellow
CSS colors that differ from Android Color class colors are:
darkgray, darkgrey, gray, grey , lightgray, , lightgrey, green
Although “white” is defined as a valid color in the Color class, its value (0xFFFFFFFF) causes processing to ignore the color altogether. This is because the value returned for “white” by the Color class (0xFFFFFFFF) is interpreted as a “not found” condition (-1).
One work-around is to specify 0xFFFFFF for the color “white” and let the processing add the leading “FF”.
This incident report ("Document Supported HTML Tags for HtmlCompat") may provide additional information in the future.
Supported html tags on Android TextView
Note:
This targets API 27. Earlier versions are NOT guaranteed to support the tags seen in this list, and same for later ones (they could be removed). See the equivalent class for the applicable API versions for the applicable list.
It seems these aren't documented anywhere (correction, 2021: no longer the case), and there isn't a concrete list of when tags are added (2021: this still appears to be the case). I figured the HTML tags would need to be declared somewhere, if not in the docs, at least in the code. Fortunately, Android's HTML parsing is straight-forward enough to easily understand the relevant bits without dumpster-diving too much.
For future versions: you can either use IntelliJ or some other IDE to explore the source code of Html.java, or you can always go to the AOSP site or the equivalent GitHub repository. Using an IDE is potentially the easiest option.
Html#fromHtml(String, int)
calls Html#fromHtml(String, Int, ImageGetter, TagHandler
. The last method creates a HtmlToSpannedConverter
, and returns the result of the conversion. I dug into that code, and I found this method:
private void handleStartTag(String tag, Attributes attributes) {
if (tag.equalsIgnoreCase("br")) {
// We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
// so we can safely emit the linebreaks when we handle the close tag.
} else if (tag.equalsIgnoreCase("p")) {
startBlockElement(mSpannableStringBuilder, attributes, getMarginParagraph());
startCssStyle(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("ul")) {
startBlockElement(mSpannableStringBuilder, attributes, getMarginList());
} else if (tag.equalsIgnoreCase("li")) {
startLi(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("div")) {
startBlockElement(mSpannableStringBuilder, attributes, getMarginDiv());
} else if (tag.equalsIgnoreCase("span")) {
startCssStyle(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("strong")) {
start(mSpannableStringBuilder, new Bold());
} else if (tag.equalsIgnoreCase("b")) {
start(mSpannableStringBuilder, new Bold());
} else if (tag.equalsIgnoreCase("em")) {
start(mSpannableStringBuilder, new Italic());
} else if (tag.equalsIgnoreCase("cite")) {
start(mSpannableStringBuilder, new Italic());
} else if (tag.equalsIgnoreCase("dfn")) {
start(mSpannableStringBuilder, new Italic());
} else if (tag.equalsIgnoreCase("i")) {
start(mSpannableStringBuilder, new Italic());
} else if (tag.equalsIgnoreCase("big")) {
start(mSpannableStringBuilder, new Big());
} else if (tag.equalsIgnoreCase("small")) {
start(mSpannableStringBuilder, new Small());
} else if (tag.equalsIgnoreCase("font")) {
startFont(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("blockquote")) {
startBlockquote(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("tt")) {
start(mSpannableStringBuilder, new Monospace());
} else if (tag.equalsIgnoreCase("a")) {
startA(mSpannableStringBuilder, attributes);
} else if (tag.equalsIgnoreCase("u")) {
start(mSpannableStringBuilder, new Underline());
} else if (tag.equalsIgnoreCase("del")) {
start(mSpannableStringBuilder, new Strikethrough());
} else if (tag.equalsIgnoreCase("s")) {
start(mSpannableStringBuilder, new Strikethrough());
} else if (tag.equalsIgnoreCase("strike")) {
start(mSpannableStringBuilder, new Strikethrough());
} else if (tag.equalsIgnoreCase("sup")) {
start(mSpannableStringBuilder, new Super());
} else if (tag.equalsIgnoreCase("sub")) {
start(mSpannableStringBuilder, new Sub());
} else if (tag.length() == 2 &&
Character.toLowerCase(tag.charAt(0)) == 'h' &&
tag.charAt(1) >= '1' && tag.charAt(1) <= '6') {
startHeading(mSpannableStringBuilder, attributes, tag.charAt(1) - '1');
} else if (tag.equalsIgnoreCase("img")) {
startImg(mSpannableStringBuilder, attributes, mImageGetter);
} else if (mTagHandler != null) {
mTagHandler.handleTag(true, tag, mSpannableStringBuilder, mReader);
}
}
Which does contain all of the supported HTML tags. This list might change in future versions (it did from the original answer I had), but you can dig through the source yourself to find it in later versions. With the above code, this is the currently supported list:
br
p
ul
li
div
span
strong
b
em
cite
dfn
i
big
small
font
blockquote
tt
a
u
del
s
strike
sub
sup
img
h1
h2
h3
h4
h5
h6
Further methods cover supported attributes (in <a href="">
, href
is an attribute):
img:
src
font:
color
face
size
is apparently not supported.
a:
href
p, ul, and div calls startBlockElement, which gives one attrib:
text-align
Something special worth noting about this is the supported align variables.
- center - standard and self-explanatory
- start - aligns left
- end - aligns right
start and end might be inverted in RTL layouts - I haven't tested.
span and p, and li calls startCssStyle, which gives access to the following style
attributes:
text-decoration
background-color or background
color
text-decoration
seems to be limited to line-through
.
How to display HTML in TextView?
You need to use Html.fromHtml()
to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
This is what you should do in Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}
And in Kotlin:
textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(html)
}
HTML tags in string for TextView
You have to use Html#fromHtml
String input = "<b>bold</b>";
myTextView.setText(Html.fromHtml(input));
Related Topics
Google Maps API V2 Class Not Found
Android Fragment Oncreateview VS. Onactivitycreated
Find the Physical Address of Exception Vector Table from Kernel Module
How Open New Activity Clicking an Item in Listview
A Pre-Populated Database Does Not Work at API 28 Throws "No Such Table" Exception
How to Check If User Is Logged in with Fb Sdk 4.0 for Android
How to Read the Sd Card Id Number
Play Youtube HTML5 Embedded Video in Android Webview
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
In Android Applications What Is The Role of "R.Java"
Android Browser Bug? Div Overflow Scrolling
Finding Android Sdk on MAC and Adding to Path
Howto Do a Simple Ftp Get File on Android