LinkAnnotation.Url


An annotation that contains a url string. When clicking on the text to which this annotation is attached, the app will try to open the url using androidx.compose.ui.platform.UriHandler. However, if linkInteractionListener is provided, its LinkInteractionListener.onClick method will be called instead and so you need to then handle opening url manually (for example by calling androidx.compose.ui.platform.UriHandler).

See also
LinkAnnotation

Summary

Public constructors

Url(
    url: String,
    styles: TextLinkStyles?,
    linkInteractionListener: LinkInteractionListener?
)
Cmn

Public functions

LinkAnnotation.Url
copy(
    url: String,
    styles: TextLinkStyles?,
    linkInteractionListener: LinkInteractionListener?
)

Returns a copy of this Url, optionally overriding some of the values.

Cmn
open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open String
Cmn

Public properties

open LinkInteractionListener?

Interaction listener triggered when user interacts with this link.

Cmn
open TextLinkStyles?

Style configuration for this link in different states.

Cmn
String
Cmn

Public constructors

Url

Url(
    url: String,
    styles: TextLinkStyles? = null,
    linkInteractionListener: LinkInteractionListener? = null
)

Public functions

copy

fun copy(
    url: String = this.url,
    styles: TextLinkStyles? = this.styles,
    linkInteractionListener: LinkInteractionListener? = this.linkInteractionListener
): LinkAnnotation.Url

Returns a copy of this Url, optionally overriding some of the values.

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

toString

open fun toString(): String

Public properties

linkInteractionListener

open val linkInteractionListenerLinkInteractionListener?

Interaction listener triggered when user interacts with this link.

import androidx.compose.foundation.text.BasicText
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withLink
import androidx.compose.ui.unit.sp

// Display a link in the text and log metrics whenever user clicks on it. In that case we handle
// the link using openUri method of the LocalUriHandler
val uriHandler = LocalUriHandler.current
BasicText(
    buildAnnotatedString {
        append("Build better apps faster with ")
        val link =
            LinkAnnotation.Url(
                "https://developer.android.com/jetpack/compose",
                TextLinkStyles(SpanStyle(color = Color.Blue))
            ) {
                val url = (it as LinkAnnotation.Url).url
                // log some metrics
                uriHandler.openUri(url)
            }
        withLink(link) { append("Jetpack Compose") }
    }
)

styles

open val stylesTextLinkStyles?

Style configuration for this link in different states.

import androidx.compose.foundation.text.BasicText
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkAnnotation
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLinkStyles
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink
import androidx.compose.ui.unit.sp

// Display a link in the text that gets an underline when hovered
BasicText(
    buildAnnotatedString {
        append("Build better apps faster with ")
        val link =
            LinkAnnotation.Url(
                "https://developer.android.com/jetpack/compose",
                TextLinkStyles(
                    style = SpanStyle(color = Color.Blue),
                    hoveredStyle = SpanStyle(textDecoration = TextDecoration.Underline)
                )
            )
        withLink(link) { append("Jetpack Compose") }
    }
)

url

val urlString