Search code examples
androidandroid-support-library

Which HTML tags are supported by the Android HTML class and how do I use HtmlCompat?


I am looking for a concise description of the HTML tags and properties supported by the Android Html class, specifically those tags processed by Html#fromHtml(). The official documentation states that "not all HTML tags are supported," but that is not really helpful. Other documentation I have found online seems to be out of date and says little, if anything, about properties that are supported.

Also, I believe that I should be using HtmlCompat instead of Html, but it is not clear to me what HtmlCompat does. The documentation states that HtmlCompat is a "backwards compatible version of Html," but what does that mean? When running on a device that has an API level below 24, I expect HtmlCompat#fromHtml() to accept the same tags as Html does on an API 24+ devices, but it does not.


Solution

  • HtmlCompat

    TL;DR See here for a more up-to-date document covering HTML support in Android. The referenced document also provides information regarding native Android support of HTML tags. (January 2024)

    The following is still largely true but is slightly out of date.

    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.

    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

    <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.