Added in API level 1

Html

open class Html
kotlin.Any
   ↳ android.text.Html

This class processes HTML strings into displayable styled text. Not all HTML tags are supported.

Summary

Nested classes
abstract

Retrieves images for HTML <img> tags.

abstract

Is notified when HTML tags are encountered that the parser does not know how to interpret.

Constants
static Int

Flags for fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler): Separate block-level elements with line breaks (single newline character) in between.

static Int

Flags for fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler): Separate block-level elements with blank lines (two newline characters) in between.

static Int

Flag indicating that CSS color values should be used instead of those defined in Color.

static Int

Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default.

static Int

Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default.

static Int

Flag indicating that texts inside <h1>~<h6> elements will be separated from other texts with one newline character by default.

static Int

Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default.

static Int

Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default.

static Int

Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default.

static Int

Option for toHtml(android.text.Spanned,int): Wrap consecutive lines of text delimited by '\n' inside <p> elements.

static Int

Option for toHtml(android.text.Spanned,int): Wrap each line of text delimited by '\n' inside a <p> or a <li> element.

Public methods
open static String!

Returns an HTML escaped representation of the given plain text.

open static Spanned!
fromHtml(source: String!)

Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.

open static Spanned!
fromHtml(source: String!, flags: Int)

Returns displayable styled text from the provided HTML string.

open static Spanned!
fromHtml(source: String!, imageGetter: Html.ImageGetter!, tagHandler: Html.TagHandler!)

Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.

open static Spanned!
fromHtml(source: String!, flags: Int, imageGetter: Html.ImageGetter!, tagHandler: Html.TagHandler!)

Returns displayable styled text from the provided HTML string.

open static String!
toHtml(text: Spanned!)

open static String!
toHtml(text: Spanned!, option: Int)

Returns an HTML representation of the provided Spanned text.

Constants

FROM_HTML_MODE_COMPACT

Added in API level 24
static val FROM_HTML_MODE_COMPACT: Int

Flags for fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler): Separate block-level elements with line breaks (single newline character) in between. This inverts the Spanned to HTML string conversion done with the option TO_HTML_PARAGRAPH_LINES_INDIVIDUAL.

Value: 63

FROM_HTML_MODE_LEGACY

Added in API level 24
static val FROM_HTML_MODE_LEGACY: Int

Flags for fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler): Separate block-level elements with blank lines (two newline characters) in between. This is the legacy behavior prior to N.

Value: 0

FROM_HTML_OPTION_USE_CSS_COLORS

Added in API level 24
static val FROM_HTML_OPTION_USE_CSS_COLORS: Int

Flag indicating that CSS color values should be used instead of those defined in Color.

Value: 256

FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE: Int

Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default.

Value: 32

FROM_HTML_SEPARATOR_LINE_BREAK_DIV

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_DIV: Int

Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default.

Value: 16

FROM_HTML_SEPARATOR_LINE_BREAK_HEADING

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_HEADING: Int

Flag indicating that texts inside <h1>~<h6> elements will be separated from other texts with one newline character by default.

Value: 2

FROM_HTML_SEPARATOR_LINE_BREAK_LIST

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_LIST: Int

Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default.

Value: 8

FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM: Int

Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default.

Value: 4

FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH

Added in API level 24
static val FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH: Int

Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default.

Value: 1

TO_HTML_PARAGRAPH_LINES_CONSECUTIVE

Added in API level 24
static val TO_HTML_PARAGRAPH_LINES_CONSECUTIVE: Int

Option for toHtml(android.text.Spanned,int): Wrap consecutive lines of text delimited by '\n' inside <p> elements. BulletSpans are ignored.

Value: 0

TO_HTML_PARAGRAPH_LINES_INDIVIDUAL

Added in API level 24
static val TO_HTML_PARAGRAPH_LINES_INDIVIDUAL: Int

Option for toHtml(android.text.Spanned,int): Wrap each line of text delimited by '\n' inside a <p> or a <li> element. This allows ParagraphStyles attached to be encoded as CSS styles within the corresponding <p> or <li> element.

Value: 1

Public methods

escapeHtml

Added in API level 16
open static fun escapeHtml(text: CharSequence!): String!

Returns an HTML escaped representation of the given plain text.

fromHtml

Added in API level 1
Deprecated in API level 24
open static fun fromHtml(source: String!): Spanned!

Deprecated: use fromHtml(java.lang.String,int) instead.

Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.

fromHtml

Added in API level 24
open static fun fromHtml(
    source: String!,
    flags: Int
): Spanned!

Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

fromHtml

Added in API level 1
Deprecated in API level 24
open static fun fromHtml(
    source: String!,
    imageGetter: Html.ImageGetter!,
    tagHandler: Html.TagHandler!
): Spanned!

Deprecated: use fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler) instead.

Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.

fromHtml

Added in API level 24
open static fun fromHtml(
    source: String!,
    flags: Int,
    imageGetter: Html.ImageGetter!,
    tagHandler: Html.TagHandler!
): Spanned!

Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).

This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

toHtml

Added in API level 1
Deprecated in API level 24
open static fun toHtml(text: Spanned!): String!

Deprecated: use toHtml(android.text.Spanned,int) instead.

toHtml

Added in API level 24
open static fun toHtml(
    text: Spanned!,
    option: Int
): String!

Returns an HTML representation of the provided Spanned text. A best effort is made to add HTML tags corresponding to spans. Also note that HTML metacharacters (such as "<" and "&") within the input text are escaped.

Parameters
text Spanned!: input text to convert
option Int: one of TO_HTML_PARAGRAPH_LINES_CONSECUTIVE or TO_HTML_PARAGRAPH_LINES_INDIVIDUAL
Return
String! string containing input converted to HTML