Clickable Text in Jetpack Compose (LinkAnnotation Examples)

To make text clickable in Jetpack Compose, use a normal Text: add Modifier.clickable when the whole text should react to a tap, or build an AnnotatedString with withLink(LinkAnnotation…) when only some words are links. The old ClickableText composable is deprecated; this guide shows the current way, with copy-paste examples.

Note: ClickableText has been deprecated since Compose 1.7. Google’s reference says to use Text or BasicText with an AnnotatedString that contains a LinkAnnotation (source). Links made this way also work with keyboards, Switch Access and TalkBack.

1. Make the whole text clickable

If tapping anywhere on the text should do one thing, a plain Modifier.clickable is enough. Give it an onClickLabel so screen readers announce what the tap does.

@Composable
fun TapMeText() {
    val context = LocalContext.current
    Text(
        text = "Tap me",
        modifier = Modifier.clickable(onClickLabel = "Show a message") {
            Toast.makeText(context, "Text tapped", Toast.LENGTH_SHORT).show()
        }
    )
}

2. Open a website from part of the text

Use buildAnnotatedString and wrap the link words in withLink(LinkAnnotation.Url(…)). Compose opens the URL for you, so you don’t need an Intent.

@Composable
fun TermsText() {
    val linkStyle = TextLinkStyles(
        style = SpanStyle(
            color = MaterialTheme.colorScheme.primary,
            textDecoration = TextDecoration.Underline
        )
    )
    Text(
        buildAnnotatedString {
            append("By continuing you agree to our ")
            withLink(LinkAnnotation.Url("https://example.com/terms", linkStyle)) {
                append("Terms")
            }
            append(" and ")
            withLink(LinkAnnotation.Url("https://example.com/privacy", linkStyle)) {
                append("Privacy Policy")
            }
            append(".")
        }
    )
}

3. Run your own code when a word is tapped

For actions that are not web links – resend a code, open a screen, show a dialog – use LinkAnnotation.Clickable with a tag and a linkInteractionListener:

@Composable
fun ResendCodeText(onResend: () -> Unit) {
    Text(
        buildAnnotatedString {
            append("Didn't get the code? ")
            withLink(
                LinkAnnotation.Clickable(
                    tag = "resend",
                    styles = TextLinkStyles(
                        SpanStyle(
                            color = MaterialTheme.colorScheme.primary,
                            fontWeight = FontWeight.Bold
                        )
                    ),
                    linkInteractionListener = { onResend() }
                )
            ) {
                append("Resend")
            }
        }
    )
}

4. Style links for pressed, hovered and focused states

TextLinkStyles takes a normal style plus optional styles for when the link is focused, hovered or pressed:

val linkStyles = TextLinkStyles(
    style = SpanStyle(color = Color(0xFF1A73E8)),
    focusedStyle = SpanStyle(background = Color(0x221A73E8)),
    hoveredStyle = SpanStyle(textDecoration = TextDecoration.Underline),
    pressedStyle = SpanStyle(color = Color(0xFF174EA6))
)

Imports used in these examples

import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
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.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withLink

Migrating from ClickableText

Old codeReplace with
ClickableText(text) { offset -> … } on the whole textText(text, Modifier.clickable { … })
pushStringAnnotation("URL", url) + getStringAnnotations in onClickwithLink(LinkAnnotation.Url(url)) { … }
Custom tag annotation for an actionwithLink(LinkAnnotation.Clickable(tag, styles) { … })
SpanStyle to color the linkTextLinkStyles(style = SpanStyle(…))

Common problems

The link doesn’t look like a link

Pass TextLinkStyles with a color and/or TextDecoration.Underline. Depending on your Compose version, links may or may not get a default style, so set it yourself for a consistent look.

Nothing happens when I tap the URL

Check that the URL includes https:// and that the device has an app (a browser) that can open it.

The whole line reacts instead of one word

You probably used Modifier.clickable. Use withLink on just the words that should be links.

Video tutorial (uses the older ClickableText API)

FAQ

Is ClickableText deprecated?

Yes. Since Compose 1.7 the recommended way is Text with an AnnotatedString that contains a LinkAnnotation.

How do I add a hyperlink to Text in Jetpack Compose?

Wrap the link words with withLink(LinkAnnotation.Url("https://…")) inside buildAnnotatedString and pass the result to Text.

How do I detect which word was clicked?

Give each clickable word its own LinkAnnotation.Clickable with a different tag and listener, instead of working out character offsets as ClickableText required.

More Jetpack Compose text guides

WhatsApp us📞 Call now