Annon
Annon

Reputation: 843

Auto scrolling muli-line textfield in compose

I've a multi-line text field as below

val scrollState = rememberScrollState(0)

TextField(
    modifier = Modifier
        .fillMaxWidth()
        .height(75.dp)
        .verticalScroll(scrollState),
    value = caption,
    onValueChange = { onCaptionChanged(it) }
)

Currently, it support manual scrolling but i want it to scroll down automatically when user enter new line (\n). How do i achieve that?

Upvotes: 9

Views: 3648

Answers (1)

Phil Dukhov
Phil Dukhov

Reputation: 87894

You can scroll using scrollState.scrollTo. To track content height change, you can check scrollState.maxValue, for example like this:

LaunchedEffect(scrollState.maxValue) {
    scrollState.scrollTo(scrollState.maxValue)
    // or 
    // scrollState.animateScrollTo(scrollState.maxValue)
}

Upvotes: 10

Related Questions