---
title: "withLink"
description: "Pushes a [LinkAnnotation] to the [AnnotatedString.Builder], executes [block] and then pops the
annotation."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-common'>Common</div>


```kotlin
inline fun <R : Any> Builder.withLink(link: LinkAnnotation, block: Builder.() -> R): R
```


Pushes a `LinkAnnotation` to the `AnnotatedString.Builder`, executes `block` and then pops the
annotation.

#### Parameters

| | |
| --- | --- |
| link | A `LinkAnnotation` object representing a clickable part of the text |
| block | function to be executed |


#### Returns

| | |
| --- | --- |
|  | result of the `block` |




## Code Examples
### AnnotatedStringWithHoveredLinkStylingSample
```kotlin
@Composable
fun AnnotatedStringWithHoveredLinkStylingSample() {
    // 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") }
        }
    )
}
```
### AnnotatedStringWithLinkSample
```kotlin
@Composable
fun AnnotatedStringWithLinkSample() {
    // 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")
            }
        }
    )
}
```
### AnnotatedStringWithListenerSample
```kotlin
@Composable
fun AnnotatedStringWithListenerSample() {
    // 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") }
        }
    )
}
```

