# LinkAnnotation

Artifact: [androidx.compose.ui:ui-text](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/text/LinkAnnotation.kt+class:androidx.compose.ui.text.LinkAnnotation) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.7.0) Common/AllAndroid/JVM
- Cmn

  ```
  abstract class LinkAnnotation : AnnotatedString.Annotation
  ```
Known direct subclasses [LinkAnnotation.Clickable](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable), [LinkAnnotation.Url](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable` | An annotation that contains a clickable marked with `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable#tag()`. |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url` | An annotation that contains a `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url#url()` string. |

*** ** * ** ***

An annotation that represents a clickable part of the text.

```kotlin
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.withLink
import androidx.compose.ui.unit.sp

// Display a link in the text
BasicText(
    buildAnnotatedString {
        append("Build better apps faster with ")
        withLink(
            LinkAnnotation.Url(
                "https://developer.android.com/jetpack/compose",
                TextLinkStyles(style = SpanStyle(color = Color.Blue)),
            )
        ) {
            append("Jetpack Compose")
        }
    }
)
```

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable : https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation` An annotation that contains a clickable marked with `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Clickable#tag()`. |
| `class https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url : https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation` An annotation that contains a `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation.Url#url()` string. |

| ### Public properties |
|---|---|---|
| `abstract https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkInteractionListener?` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation#linkInteractionListener()` Interaction listener triggered when user interacts with this link. | Cmn |
| `abstract https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextLinkStyles?` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/LinkAnnotation#styles()` Style configuration for this link in different states. | Cmn |

## Public properties

### linkInteractionListener

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.7.0)

```
abstract val linkInteractionListener: LinkInteractionListener?
```

Interaction listener triggered when user interacts with this link.

```kotlin
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

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.7.0)

```
abstract val styles: TextLinkStyles?
```

Style configuration for this link in different states.

```kotlin
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") }
    }
)
```