2

I want to show a dialog and automatically set focus to a OutlinedTextField, so user can instantly start typing.
I ended up with text field does get focus, does get the cursor flickering, but the keyboard remains hidden. So user still has to click on a textField to make the keyboard appear. Here is how I'm doing this

    LaunchedEffect(Unit) {
        focusRequester.requestFocus()
    }

OutlinedTextField(
                        value = text,
                        modifier = Modifier
                            .focusRequester(focusRequester)
                            .fillMaxWidth(),
                        onValueChange = {
                            text = it
                        }
                    )
Waldmann
  • 1,563
  • 12
  • 25

1 Answers1

4

To make keyboard show up, you should place a delay before requesting the focus:

LaunchedEffect(Unit) {
    delay(200)// <-- This is crucial.
    focusRequester.requestFocus()
}

OutlinedTextField(
    value = text,
    modifier = Modifier
        .focusRequester(focusRequester)
        .fillMaxWidth(),
    onValueChange = {
        text = it
    }
)

The delay time may be changed. For me it starts working starting from 100ms. If it still doesn't work with 200, increase it until it works. I believe it's all about performance of the devcie, so the higher the delay, the more slow devices can be used.

Thracian
  • 43,021
  • 16
  • 133
  • 222
Waldmann
  • 1,563
  • 12
  • 25