Skip to content

Instantly share code, notes, and snippets.

@burkido
Created March 23, 2024 15:03
Show Gist options
  • Save burkido/b830c7b5b01cc491fe442bbb5e8d1207 to your computer and use it in GitHub Desktop.
Save burkido/b830c7b5b01cc491fe442bbb5e8d1207 to your computer and use it in GitHub Desktop.
Usage of clickable annotated text.
val annotatedString = buildAnnotatedString {
append(stringResource(id = R.string.prefix_terms))
pushStringAnnotation(tag = stringResource(id = R.string.terms), annotation = "https://termify.io/terms-and-conditions-generator")
withStyle(
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
)
) {
append(stringResource(id = R.string.terms))
}
append(stringResource(id = R.string.and))
pushStringAnnotation(tag = stringResource(id = R.string.privacy_policy), annotation = "https://termify.io/privacy-policy-generator")
withStyle(
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
)
) {
append(stringResource(id = R.string.privacy_policy))
}
pop()
}
ClickableText(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
text = annotatedString,
style = MaterialTheme.typography.bodyMedium,
onClick = { offset ->
annotatedString.getStringAnnotations(tag = "policy", start = offset, end = offset)
.firstOrNull()?.let {
Timber.d("policy URL: ${it.item}")
}
annotatedString.getStringAnnotations(tag = "terms", start = offset, end = offset)
.firstOrNull()?.let {
Timber.d("terms URL: ${it.item}")
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment