Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextField is overlapped by keyboard in Android Compose

I have a TextField in column with verticalScroll(). When adding a large number of characters, the textfield size goes beyond the keyboard and I stop seeing what I am typing I tried to use this lib, but that's doesn't help

like image 422
Tus Die Avatar asked Oct 25 '25 02:10

Tus Die


1 Answers

I think you can use BringIntoViewRequester in your TextField.

var state by rememberSaveable {
    mutableStateOf("")
}
val coroutineScope = rememberCoroutineScope()
val bringIntoViewRequester = remember {
    BringIntoViewRequester()
}

TextField(
    value = state,
    onValueChange = { text ->
        state = text
        // This will cause the TextField be repositioned on the screen
        // while you're typing
        coroutineScope.launch {
            bringIntoViewRequester.bringIntoView()
        }
    },
    modifier = Modifier
        .bringIntoViewRequester(bringIntoViewRequester)
        .onFocusChanged {
            if (it.isFocused) {
                coroutineScope.launch {
                    delay(400) // delay to way the keyboard shows up
                    bringIntoViewRequester.bringIntoView()
                }
            }
        },
)

See the complete sample here.

like image 152
nglauber Avatar answered Oct 26 '25 16:10

nglauber