https://kotlinlang.org logo
Title
e

Eric Ampire [MOD]

08/07/2021, 11:30 AM
Hello, does someone has an Idea about how to count lines of a string in order to show or not see more button using compose like on the following screenshot ?
Sometimes the number of lines depends on the width of the screen or the orientation (landscape or portrait)
a

Abhishek Dewan

08/07/2021, 2:56 PM
you can just set a lineCount value on the text and track the state internally ! essentially create a custom text composable that has a state that tracks whether the button is shown or not and then toggle it. https://github.com/abhishekdewan101/Scout/blob/782c088bbd79eab3581560020a9d4b3ccdf7f068/app/src/main/java/com/abhishek101/gamescout/features/main/details/GameDetailScreen.kt#L311 Heres and example
r

Ravi

08/07/2021, 5:14 PM
@Eric Ampire [MOD] u can get line count using
onTextLayout
in
Text
val lines = remember { mutableStateOf(0)}
Text(onTextLayout = {                               
                 lines.value = it.lineCount
               },text="lsadfjalsd")
e

Eric Ampire [MOD]

08/07/2021, 6:07 PM
Thanks very much, Iet me tried