https://kotlinlang.org logo
#compose
Title
# compose
t

Tepes Lucian Victor

01/27/2023, 7:29 PM
Does anyone have an issue with a
TextField
inside a
DialogFragment
not showing the soft keyboard when focused? I swear it worked at some point but I’ve tried tried reverting / updating compose version and nothing seems to fix it.
g

gildor

01/28/2023, 3:03 PM
Yep, it requires adding window flags which prevents showing keyboard. Dialog has special internal hack, it adds those flags when it detects that EditText is inflated, but doesn't do this with compose Not near my computer, do not remember which flags, but probably you can check window flags related to IME
g

gildor

02/01/2023, 8:32 AM
Why use custom view if you can just add flags on Dialog Fragment?
t

Tepes Lucian Victor

02/01/2023, 8:34 AM
I’ve tried the flags and they don’t work
Funny enough on older devices the keyboard worked (tested on an old nexus 5 with android 6). I’ve digged in compose source code to find the workaround.
g

gildor

02/01/2023, 8:43 AM
Definitely works for us with Android 6+
Copy code
class MyDialog : androidx.fragment.app.DialogFragment() {
...
    override fun onResume() {
        super.onResume()
        // AlertDialog sets flag to prevent showing keyboard if custom view doesn't have EditText
        // It of course cannot be detected with Compose (all composition is dynamic)
        // So clear flags, show keyboard
        // <https://stackoverflow.com/a/62767205/420412>
        val window = dialog?.window
        if (window != null) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
            window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return ComposeView(requireContext()).apply {
            setContent {
                MyComposable()
            }
        }
    }
}
3 Views