https://kotlinlang.org logo
#compose
Title
# compose
k

Kulwinder Singh

11/26/2021, 2:06 AM
Copy code
var readMore by remember { mutableStateOf(false) }
var clickText by remember { mutableStateOf("Read More") }
var showClickButton by remember { mutableStateOf(false) }
val annotatedText = buildAnnotatedString {
    withStyle(style = SpanStyle(color = MaterialTheme.colors.OnSurface800)) {
        append(text + " ")
    }
    
    if (showClickButton) {
        pushStringAnnotation("click", "click")
        withStyle(style = SpanStyle(color = MaterialTheme.colors.Interactive500)) {
            append(clickText)
        }
        pop()
    }
}

ClickableText(
        text = annotatedText,
        style = MaterialTheme.typography.smallBody,
        overflow = TextOverflow.Ellipsis,
        maxLines = if (readMore) Int.MAX_VALUE else 2,
        onTextLayout = { showClickButton = it.hasVisualOverflow }
    )
I am trying to “Read More” button at the end of text view but looks like
buildAnnotatedString
does not recompose when
showClickButton
or state change, so here how can i achieve this ?
a

agrosner

11/26/2021, 2:10 AM
derivedStateOf
k

Kulwinder Singh

11/26/2021, 2:19 AM
Can you please share some sample or source of how i can achieve it
k

Kulwinder Singh

11/26/2021, 2:56 AM
Sorry, i don’t think it will work in my case
z

Zach Klippenstein (he/him) [MOD]

11/29/2021, 1:17 AM
Hm, that should still work even without
derivedStateOf
(although using
derivedStateOf
would still probably be more efficient)
Is your function recomposing when the text layout changes?
k

Kulwinder Singh

12/01/2021, 6:28 AM
@Zach Klippenstein (he/him) [MOD] no above code is not working
z

Zach Klippenstein (he/him) [MOD]

12/01/2021, 6:31 PM
“not working” can mean a lot of different things 😉
i copied your code and it works as expected for me
i would put some breakpoints or log statements in and see if
onTextLayout
is getting called and giving you the values you expect
2 Views