Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose focus requester not working with Dialog

I'm using the below code to try and request focus to a textfield and have the keyboard come up. Currently the textfield does request focus but the keyboard fails to show. This same code works in another project im working on, but the difference here is this code is inside a Dialog composable, and the other code isn't, so I'm not sure if its the Dialog making the keyboard fail to show?

val textField = remember { FocusRequester() }

 Dialog(onDismissRequest = {
    openDialog.value = false
    dialogInput.value = ""
}) {

    Column(
        modifier = Modifier
            .height(274.dp)
            .background(Color.Transparent)
            .clickable {
                openDialog.value = false
                dialogInput.value = ""
            }
    ) {

        OutlinedTextField(
            modifier = Modifier
                .height(64.dp)
                .background(Color.White)
                .focusRequester(textField),
            label = {
                Text(
                    text = label,
                    style = MaterialTheme.typography.body2.copy(color = Color.Black)
                )
            },
            value = dialogInput.value,
            onValueChange = {
                dialogInput.value = it
                events.filterPlayers(it)
            },
            textStyle = MaterialTheme.typography.body2.copy(color = Color.Black),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.White,
                unfocusedIndicatorColor = Color.White,
                focusedIndicatorColor = Color.White
            )
        )

        DisposableEffect(Unit) {
            textField.requestFocus()
            onDispose {}
        }
}
like image 267
alfietap Avatar asked Feb 13 '26 08:02

alfietap


1 Answers

val focusRequester = FocusRequester()
LocalView.current.viewTreeObserver.addOnWindowFocusChangeListener {
    if (it) focusRequester.requestFocus()
}

this work for me, after window for dialog get focused, request focus for TextField would show soft keyboard automatically.

like image 63
lllwwwbbb Avatar answered Feb 15 '26 22:02

lllwwwbbb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!