I have created an annotated string in Jetpack composable and assigned it to composable Text view. It has multiple hyperlinks. The code is as below:
@Composable
fun HyperlinkText(
fullText: String,
linkTexts: List<String>,
linkUris: List<String>,
linkTextColor: Color,
textColor: Color
) {
val annotatedString = buildAnnotatedString {
append(fullText)
linkTexts.map {
val startIndex = fullText.indexOf(it)
if (startIndex != -1) {
val endIndex = startIndex + it.length
if (endIndex > fullText.length) return@map
addStyle(
SpanStyle(
color = linkTextColor
),
startIndex,
endIndex
)
val indexOfLinkText = linkTexts.indexOf(it)
if (indexOfLinkText != -1) {
addStringAnnotation(
tag = ANNOTATION_URL,
annotation = linkUris[indexOfLinkText],
start = startIndex,
end = endIndex
)
}
}
}
}
val uriHandler = LocalUriHandler.current
var layoutResult by remember { mutableStateOf<TextLayoutResult?>(null) }
Text(
text = annotatedString,
style = run { deviceTypography?.resolveStyle() ?: typography.resolveStyle() }.copy(
color = textColor
),
modifier = Modifier
.pointerInput(Unit) {
detectTapGestures { offset ->
layoutResult?.let { textLayoutResult ->
val position = textLayoutResult.getOffsetForPosition(offset)
annotatedString.getStringAnnotations(
tag = ANNOTATION_URL,
start = position,
end = position
).firstOrNull()?.let { stringAnnotation ->
uriHandler.openUri(stringAnnotation.item)
}
}
}
}
.focusable(),
onTextLayout = { layoutResult = it }
)
}
When navigating using hardware keyboard, the focus never goes on the hyperlink of the annotated string. I am using jetpack compose 1.7.0. I tried mix and match of focusables and focusProperties in modifier, but nothing works. What could be the issue?