# HtmlCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/text/HtmlCompat.java+class:androidx.core.text.HtmlCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public final class HtmlCompat
```

<br />

*** ** * ** ***

Backwards compatible version of `https://developer.android.com/reference/android/text/Html.html`.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_MODE_COMPACT() = 63` Flags for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#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 final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_MODE_LEGACY() = 0` Flags for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#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 final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_OPTION_USE_CSS_COLORS() = 256` Flag indicating that CSS color values should be used instead of those defined in `https://developer.android.com/reference/android/graphics/Color.html`. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE() = 32` Flag indicating that texts inside `<blockquote>` elements will be separated from other texts with one newline character by default. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_DIV() = 16` Flag indicating that texts inside `<div>elements will be separated from other texts with one newline character by default.` |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_HEADING() = 2` Flag indicating that texts inside `<h1>`\~`<h6>` elements will be separated from other texts with one newline character by default. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_LIST() = 8` Flag indicating that texts inside `<ul>` elements will be separated from other texts with one newline character by default. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM() = 4` Flag indicating that texts inside `<li>` elements will be separated from other texts with one newline character by default. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH() = 1` Flag indicating that texts inside `<p>` elements will be separated from other texts with one newline character by default. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#TO_HTML_PARAGRAPH_LINES_CONSECUTIVE() = 0` Option for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int)`: Wrap consecutive lines of text delimited by '\\n' inside `<p>` elements. |
| `static final int` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#TO_HTML_PARAGRAPH_LINES_INDIVIDUAL() = 1` Option for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int)`: Wrap each line of text delimited by '\\n' inside a `<p>` or a `<li>` element. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/text/Spanned.html` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html source, int flags)` Invokes `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, int)` on API 24 and newer, otherwise `flags` are ignored and `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)` is used. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/text/Spanned.html` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int,android.text.Html.ImageGetter,android.text.Html.TagHandler)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html source, int flags, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/Html.ImageGetter.html imageGetter, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/text/Html.TagHandler.html tagHandler )` Invokes `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler)` on API 24 or newer, otherwise `flags` are ignored and `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, android.text.Html.ImageGetter, android.text.Html.TagHandler)` is used. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/core/text/HtmlCompat#toHtml(android.text.Spanned,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/text/Spanned.html text, int options)` Invokes `https://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned, int)` on API 24 or newer, otherwise `options` are ignored and `https://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)` is used. |

## Constants

### FROM_HTML_MODE_COMPACT

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_MODE_COMPACT = 63
```

Flags for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#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 `https://developer.android.com/reference/android/text/Spanned.html` to HTML string conversion done with the option `https://developer.android.com/reference/androidx/core/text/HtmlCompat#TO_HTML_PARAGRAPH_LINES_INDIVIDUAL()`.

### FROM_HTML_MODE_LEGACY

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_MODE_LEGACY = 0
```

Flags for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#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.

### FROM_HTML_OPTION_USE_CSS_COLORS

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256
```

Flag indicating that CSS color values should be used instead of those defined in `https://developer.android.com/reference/android/graphics/Color.html`.

### FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32
```

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

### FROM_HTML_SEPARATOR_LINE_BREAK_DIV

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16
```

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

### FROM_HTML_SEPARATOR_LINE_BREAK_HEADING

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2
```

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

### FROM_HTML_SEPARATOR_LINE_BREAK_LIST

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8
```

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

### FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4
```

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

### FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1
```

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

### TO_HTML_PARAGRAPH_LINES_CONSECUTIVE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0
```

Option for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int)`: Wrap consecutive lines of text delimited by '\\n' inside `<p>` elements. `https://developer.android.com/reference/android/text/style/BulletSpan.html`s are ignored.

### TO_HTML_PARAGRAPH_LINES_INDIVIDUAL

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1
```

Option for `https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,int)`: Wrap each line of text delimited by '\\n' inside a `<p>` or a `<li>` element. This allows `https://developer.android.com/reference/android/text/style/ParagraphStyle.html`s attached to be encoded as CSS styles within the corresponding `<p>` or `<li>` element.

## Public methods

### fromHtml

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @NonNull Spanned fromHtml(@NonNull String source, int flags)
```

Invokes `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, int)` on API 24 and newer, otherwise `flags` are ignored and `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)` is used.

### fromHtml

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @NonNull Spanned fromHtml(
    @NonNull String source,
    int flags,
    @Nullable Html.ImageGetter imageGetter,
    @Nullable Html.TagHandler tagHandler
)
```

Invokes `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler)` on API 24 or newer, otherwise `flags` are ignored and `https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String, android.text.Html.ImageGetter, android.text.Html.TagHandler)` is used.

### toHtml

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @NonNull String toHtml(@NonNull Spanned text, int options)
```

Invokes `https://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned, int)` on API 24 or newer, otherwise `options` are ignored and `https://developer.android.com/reference/android/text/Html.html#toHtml(android.text.Spanned)` is used.