Jetpack Compose focus requester not working with Dialog

Viewed 854

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 {}
        }
}
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.

Related