Jetpack Compose ClickableText in clickable component

3k views Asked by At

I have a text with a link in clickable card. I followed this solution to open the link from the text.

 val htmlText = description.parseHtml()
    OutlinedCard(
        onClick = {}
    ) {
        ClickableText(
            text = htmlText,
            onClick = { offset ->
                htmlText.getStringAnnotations(
                    tag = "policy",
                    start = offset,
                    end = offset
                ).firstOrNull()?.let { stringAnnotation ->
                    uriHandler.openUri(stringAnnotation.item)
                }
            })
    }

The link is working fine when I click on it. The problem is that all the text (which fills the card) is now clickable, so the card does not detect gesture anymore.

What I want is that only the portion of the text corresponding to the link be clickable, not all the text.

In the implementation of ClickableText, this modifier is added :

 val pressIndicator = Modifier.pointerInput(onClick) {
        detectTapGestures { pos ->
            layoutResult.value?.let { layoutResult ->
                onClick(layoutResult.getOffsetForPosition(pos))
            }
        }
    }

I don't know if I can modify it to support what I need or if I need a different solution.

I think this is a pretty common case, and it worked fine in xml with LinkMovementMethod.

0

There are 0 answers