oyeraghib
oyeraghib

Reputation: 1073

How to open keyboard automatically in EditTextPreference?

I have multiple EditTextPreference in my project and I want to open keyboard automatically once I tap on any of the options. Right now I have to tap on the empty space to bring out the keyboard

enter image description here

This is one of the options.

I have tried using this : https://stackoverflow.com/a/14759538/9062752 but clearly it doesn't work because it is an edit text preference and not an edit text, so I cannot access it with an ID instead I have to use a key.

Upvotes: 1

Views: 47

Answers (1)

Tenfour04
Tenfour04

Reputation: 93902

From browsing the source code, I think you could override onDisplayPreferenceDialog in your PreferenceFragment to get a reference to the DialogFragment and then the dialog. Then you could follow the code from the question you linked. Something like this (I didn't test it):

override fun onDisplayPreferenceDialog(preference: Preference) {
    super.onDisplayPreferenceDialog(preference)
    parentFragmentManager.fragments
        .filterIsInstance<EditTextPreferenceDialogFragmentCompat>()
        .firstOrNull { it.isVisible }
        ?.dialog
        ?.window?.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE)
}

Upvotes: 0

Related Questions