Thread
#compose
    j

    julioromano

    1 year ago
    Can
    LocalSoftwareKeyboardController
    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.
    r

    Ralston Da Silva

    1 year ago
    Since you are using an EditText, instead of a focusRequester, you can call requestFocus on the view. You might need to use postDelayed:
    @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)
        }
    }
    j

    julioromano

    1 year ago
    Yeah, this is the only way I could to get it working 😞 I was just hoping I could do away with
    InputMethodManager
    inside composables and use
    LocalSoftwareKeyboardController
    or
    FocusRequester
    instead.
    Sean McQuillan [G]

    Sean McQuillan [G]

    1 year ago
    This is an interesting edge to AndroidView integration. Basically, we keep our own keyboard state in Compose as Android is only interacting with the root view. SoftwareKeyboardController can only control the keyboard if compose is tracking an input connection (textfield focus). I'll open a bug https://issuetracker.google.com/issues/184947932
    If you have a repro showing the FocusRequester behavior, can you share it there?
    j

    julioromano

    1 year ago
    I’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]

    Sean McQuillan [G]

    1 year ago
    Ah must have, my bad sorry let me make a public bug for it
    j

    julioromano

    1 year ago
    It throws:
    java.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).