1

I feel that there is a better way to do this thing, I know that this code works, but I just want to know if there is another (best) way to do this.

enter image description here

Row() {

    Text(text = "By signing up, you agree with the ")
    Text(
        text = "Terms of Service",
        modifier = Modifier.clickable { },
        color = Color.Blue
    )
}

Row(
modifier = Modifier.offset(y = (-20).dp)
){

    Text(text = "and ")
    Text(
        text = "Privacy Policy",
        modifier = Modifier
            .clickable { },
        color = Color.Blue
    )
}
Edric
  • 24,639
  • 13
  • 81
  • 91
Unknown
  • 187
  • 7

1 Answers1

0

You can use an AnnotatedString and the UriHandler to open the url.

Something like:

val annotatedString = buildAnnotatedString {
    append("By signing up, you agree with the ")

    pushStringAnnotation(tag = "terms", annotation = "https://....")
    withStyle(style = SpanStyle(color = Blue)) {
        append("Terms of Service")
    }
    pop()

    append(" and ")

    pushStringAnnotation(tag = "policy", annotation = "https://....")
    withStyle(style = SpanStyle(color = Blue)) {
        append("Privacy Policy")
    }
    pop()

}
val uriHandler = LocalUriHandler.current

ClickableText(
    text = annotatedString,
    onClick = { offset ->
        annotatedString.getStringAnnotations(tag = "terms", start = offset, end = offset).firstOrNull()?.let {
            uriHandler.openUri(it.item)
        }
        annotatedString.getStringAnnotations(tag = "policy", start = offset, end = offset).firstOrNull()?.let {
            uriHandler.openUri(it.item)
        }
})

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • what is `pop()` ? – Kotlin Learner Sep 20 '22 at 16:41
  • @vivekmodi you can check the [doc](https://developer.android.com/reference/kotlin/androidx/compose/ui/text/AnnotatedString.Builder#pop(kotlin.Int)) Ends the styles or annotation up to and including the pushStyle or pushStringAnnotation that returned the given index. – Gabriele Mariotti Sep 20 '22 at 17:52
  • Thank you. @GabrieleMariotti. Can you please help me on this [issue](https://stackoverflow.com/q/73789740/11560810) – Kotlin Learner Sep 20 '22 at 19:44