Does anyone have an issue with a `TextField` insid...
# compose
t
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
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
Why use custom view if you can just add flags on Dialog Fragment?
t
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
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()
            }
        }
    }
}