julioromano
04/07/2021, 9:30 PMLocalSoftwareKeyboardController
and AndroidView
work together?
I’m trying to show the keyboard when the only editable text is inside an AndroidView
.
In the update block of AndroidView
I first call it.requestFocus()
then keyboardController?.show()
but nothing happens.
If instead of it.requestFocus()
on the view I try to set a focusRequester
modifier on the Android view and then call focusRequester.requestFocus()
I get an IllegalStateException: FocusRequester is not initialized. But I’d love the keyboard to show up as soon as the composable is first shown on screen.Ralston Da Silva
04/09/2021, 12:00 AM@Composable
fun EditTextInteropDemo(){
AndroidView({
EditText(it).apply{
postDelayed({ requestFocus(); showKeyboard() }, 100)
}
})
}
fun View.showKeyboard(){
val InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE)
if(InputMethodManager is InputMethodManager){
InputMethodManager.showSoftInput(this, SHOW_IMPLICIT)
}
}
julioromano
04/09/2021, 7:14 AMInputMethodManager
inside composables and use LocalSoftwareKeyboardController
or FocusRequester
instead.Sean McQuillan [G]
04/09/2021, 4:22 PMSean McQuillan [G]
04/09/2021, 4:23 PMjulioromano
04/12/2021, 7:08 AMI’ll open a bug https://issuetracker.google.com/issues/184947932 (edited)Access denied. Perhaps it’s been created as an internal only bug?
Sean McQuillan [G]
04/12/2021, 5:13 PMSean McQuillan [G]
04/12/2021, 5:16 PMjulioromano
04/13/2021, 3:20 PMjava.lang.IllegalStateException: FocusRequester is not initialized. One reason for this is that you requesting focus changes during composition. Focus requesters should not be made during composition, but should be made in response to some event.
at androidx.compose.ui.focus.FocusRequester.requestFocus(FocusRequester.kt:45)
But while building a PoC for the bug I couldn’t reproduce.
I suspect it’s because in the app I’m working on I call FocusRequester.requestFocus()
at the same time an animation is running. This might explain the runtime crash.
In an empty app it doesn’t crash, but FocusRequester.requestFocus()
does nothing (added the PoC code to the bug).