public
class
Html
extends Object
java.lang.Object | |
↳ | android.text.Html |
This class processes HTML strings into displayable styled text. Not all HTML tags are supported.
Nested classes | |
---|---|
interface |
Html.ImageGetter
Retrieves images for HTML <img> tags. |
interface |
Html.TagHandler
Is notified when HTML tags are encountered that the parser does not know how to interpret. |
Constants | |
---|---|
int |
FROM_HTML_MODE_COMPACT
Flags for |
int |
FROM_HTML_MODE_LEGACY
Flags for |
int |
FROM_HTML_OPTION_USE_CSS_COLORS
Flag indicating that CSS color values should be used instead of those defined in
|
int |
FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE
Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default. |
int |
FROM_HTML_SEPARATOR_LINE_BREAK_DIV
Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default. |
int |
FROM_HTML_SEPARATOR_LINE_BREAK_HEADING
Flag indicating that texts inside <h1>~<h6> elements will be separated from other texts with one newline character by default. |
int |
FROM_HTML_SEPARATOR_LINE_BREAK_LIST
Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default. |
int |
FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM
Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default. |
int |
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH
Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default. |
int |
TO_HTML_PARAGRAPH_LINES_CONSECUTIVE
Option for |
int |
TO_HTML_PARAGRAPH_LINES_INDIVIDUAL
Option for |
Public methods | |
---|---|
static
String
|
escapeHtml(CharSequence text)
Returns an HTML escaped representation of the given plain text. |
static
Spanned
|
fromHtml(String source, int flags)
Returns displayable styled text from the provided HTML string. |
static
Spanned
|
fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
This method was deprecated
in API level 24.
use |
static
Spanned
|
fromHtml(String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string. |
static
Spanned
|
fromHtml(String source)
This method was deprecated
in API level 24.
use |
static
String
|
toHtml(Spanned text, int option)
Returns an HTML representation of the provided Spanned text. |
static
String
|
toHtml(Spanned text)
This method was deprecated
in API level 24.
use |
Inherited methods | |
---|---|
From
class
java.lang.Object
|
int FROM_HTML_MODE_COMPACT
Flags for fromHtml(String, int, ImageGetter, 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
.
Constant Value: 63 (0x0000003f)
int FROM_HTML_MODE_LEGACY
Flags for fromHtml(String, int, ImageGetter, TagHandler)
: Separate block-level
elements with blank lines (two newline characters) in between. This is the legacy behavior
prior to N.
Constant Value: 0 (0x00000000)
int FROM_HTML_OPTION_USE_CSS_COLORS
Flag indicating that CSS color values should be used instead of those defined in
Color
.
Constant Value: 256 (0x00000100)
int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE
Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default.
Constant Value: 32 (0x00000020)
int FROM_HTML_SEPARATOR_LINE_BREAK_DIV
Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default.
Constant Value: 16 (0x00000010)
int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING
Flag indicating that texts inside <h1>~<h6> elements will be separated from other texts with one newline character by default.
Constant Value: 2 (0x00000002)
int FROM_HTML_SEPARATOR_LINE_BREAK_LIST
Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default.
Constant Value: 8 (0x00000008)
int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM
Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default.
Constant Value: 4 (0x00000004)
int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH
Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default.
Constant Value: 1 (0x00000001)
int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE
Option for toHtml(Spanned, int)
: Wrap consecutive lines of text delimited by '\n'
inside <p> elements. BulletSpan
s are ignored.
Constant Value: 0 (0x00000000)
int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL
Option for toHtml(Spanned, int)
: Wrap each line of text delimited by '\n' inside a
<p> or a <li> element. This allows ParagraphStyle
s attached to be
encoded as CSS styles within the corresponding <p> or <li> element.
Constant Value: 1 (0x00000001)
String escapeHtml (CharSequence text)
Returns an HTML escaped representation of the given plain text.
Parameters | |
---|---|
text |
CharSequence
|
Returns | |
---|---|
String |
Spanned fromHtml (String source, int flags)
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.
Parameters | |
---|---|
source |
String
|
flags |
int
|
Returns | |
---|---|
Spanned |
Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
This method was deprecated
in API level 24.
use fromHtml(String, int, ImageGetter, TagHandler)
instead.
Returns displayable styled text from the provided HTML string with the legacy flags
FROM_HTML_MODE_LEGACY
.
Parameters | |
---|---|
source |
String
|
imageGetter |
Html.ImageGetter
|
tagHandler |
Html.TagHandler
|
Returns | |
---|---|
Spanned |
Spanned fromHtml (String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
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.
Parameters | |
---|---|
source |
String
|
flags |
int
|
imageGetter |
Html.ImageGetter
|
tagHandler |
Html.TagHandler
|
Returns | |
---|---|
Spanned |
Spanned fromHtml (String source)
This method was deprecated
in API level 24.
use fromHtml(String, int)
instead.
Returns displayable styled text from the provided HTML string with the legacy flags
FROM_HTML_MODE_LEGACY
.
Parameters | |
---|---|
source |
String
|
Returns | |
---|---|
Spanned |
String toHtml (Spanned text, int option)
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 |
Returns | |
---|---|
String |
string containing input converted to HTML |
String toHtml (Spanned text)
This method was deprecated
in API level 24.
use toHtml(Spanned, int)
instead.
Parameters | |
---|---|
text |
Spanned
|
Returns | |
---|---|
String |