fromHtml

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

    htmlStringHTML-tagged string to be parsed to construct AnnotatedString
    linkStylesstyle configuration to be applied to links present in the string in different styles
    linkInteractionListenera 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