Created
March 23, 2024 15:03
-
-
Save burkido/b830c7b5b01cc491fe442bbb5e8d1207 to your computer and use it in GitHub Desktop.
Usage of clickable annotated text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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