Function
Android
fun AnnotatedString.Companion.fromHtml(
    htmlString: String,
    linkStyles: TextLinkStyles? = null,
    linkInteractionListener: LinkInteractionListener? = null,
): AnnotatedString

Converts a string with HTML tags into AnnotatedString.

If you define your string in the resources, make sure to use HTML-escaped opening brackets &amp;lt; instead of <.

For a list of supported tags go check Styling with HTML markup(https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML) guide.

To support nested bullet list, the nested sub-list wrapped in

    tag MUST be placed inside a list item with a tag
  • . In other words, you must add wrapped sub-list in between opening and closing
  • tag of the wrapping sub-list. This is due to the specificities of the underlying XML/HTML parser.

    Note that any link style passed directly to this method will be merged with the styles set directly on a HTML-tagged string. For example, if you set a color of the link via the span annotation to "red" but also pass a green color via the linkStyles, the link will be displayed as green. If, however, you pass a green background via the linkStyles instead, the link will be displayed as red on a green background.

    Example of displaying styled string from resources

    Parameters

    htmlString HTML-tagged string to be parsed to construct AnnotatedString
    linkStyles style configuration to be applied to links present in the string in different styles
    linkInteractionListener a listener that will be attached to links that are present in the string and triggered when user clicks on those links. When set to null, which is a default, the system will try to open the corresponding links with the androidx.compose.ui.platform.UriHandler composition local

    Code Examples

    AnnotatedStringFromHtml

    @Composable
    fun AnnotatedStringFromHtml() {
        // First, download a string as a plain text using one of the resources' methods. At this stage
        // you will be handling plurals and formatted strings in needed. Moreover, the string will be
        // resolved with respect to the current locale and available translations.
        val string = stringResource(id = R.string.example)
        // Next, convert a string marked with HTML tags into AnnotatedString to be displayed by Text
        val styledAnnotatedString = AnnotatedString.fromHtml(htmlString = string)
        BasicText(styledAnnotatedString)
    }