I like to change the background color of a link wh...
# compose
t
I like to change the background color of a link when being tapped and reset it when the finger is lifted. Here is a library which makes this possible for the former view system: https://github.com/saket/Better-Link-Movement-Method Is that possible with
ClickableText#style
?
Copy code
ClickableText(
    text = annotatedString,
    style = TextStyle(
        background = tapColor // <-- only change on tap
    ),
    onClick = {
        annotatedString
            .getStringAnnotations(tag = tag, start = it, end = it)
            .firstOrNull()
            ?.let { range -> onClick(range.item) }
    }
)
I already tried the following without success: •
interactionSource.collectIsPressedAsState()
pointerInput(Unit) { detectTapGestures { ... } }
PressInteraction
: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1636513723218600?thread_ts=1636478382.181200&cid=CJLTWPH7S • `rememberUpdatedState`: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1671519187099769
z
ClickableText is going to be deprecated in 1.7, we have proper link support now. cc @Anastasia [G]
t
@Zach Klippenstein (he/him) [MOD] Which composable do you recommend instead?
a
The new API doesn't support the pressed state (yet), only hovered and focused. This is mostly because we didn't have a signal that pressed is needed right away. Though I've just made the changes and sent it for review. So for the new API you don't need a special composable, you just use the
Text
. The key is to add the
LinkAnnotation
to your annotated string.
👍 1
t
Thanks. I guess this will be released with a future version of compose-bom then?! What is the current state of the art to do what I described?
a
You'd need to put the gesture on a
Text
and also get the
TextLayoutResult
. These two steps implemented in this sample https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]ation/samples/ClickableTextSample.kt;l=48?q=LongClickableText Use the
TextLayoutResult
to calculate the position where to draw the background
👀 1