Show soft keyboard for dialog
Asked Answered
M

6

85

I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.

InputMethodManager imm =
 (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);

The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.

I also tried this code

dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
    public void onFocusChange (View v, boolean hasFocus)
    {
        if (hasFocus)
        {
            Main.log("here");
            dialogInput.getWindow().setSoftInputMode(
              WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            /*
                InputMethodManager mgr =
                  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                mgr.showSoftInput(dialogField,0);
            */
        }
    }
});

in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.

I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.

Mauramauralia answered 23/11, 2010 at 16:56 Comment(4)
Have you tried the SHOW_IMPLICIT flag?Meandrous
This is something I have struggled with excessively but have been unable to get to work properly.Sunnysunproof
Yes, I tried the SHOW_IMPLICIT flag. The documentation says exactly, that you should call showSoftInput, if the user is expected to do input for a TextEdit. But how?Mauramauralia
This was answered here, and it works great for me.Lethia
B
201

Awesome question, I was trying to do that too and found a solution.

Using the dialog builder class AlertDialog.Builder you will have to invoke the dialog like this:

AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;

builder.set...

dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

This worked fine for me.

Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.

Bemire answered 24/10, 2013 at 17:53 Comment(7)
I do wonder how this affects the window behaviour after the dialog will close... let's not forget that the window contains the dialog and your activities as wellMonzon
This does not set the keyboard to an "always on" behaviour, this only triggers the open keyboard method while dialog is opening. Once user presses the "back key" or leaves the input field, keyboard will close again, like it normaly would.Bemire
For anyone with the same issue note that setSoftInputMode() must be called before show().Heptamerous
for me worked with 'SOFT_INPUT_STATE_ALWAYS_VISIBLE'Chlorite
not working for me, still opening the keyboard over the all the elements... using AlertDialogBuilder =(Berne
Wow, thank you so much! Can we please add a donation or contribute button to SO for showing appreciation?Tungstic
FINALLY an answer! For anyone, who wants to use it from a custom dialog class, just call getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); from the onCreate() method.Seanseana
H
17
 AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Hartung answered 2/7, 2018 at 17:28 Comment(3)
Dude you are amazing! That windows.cl3wrFlags line is important. I had checked every SO answer and that line wasn't mentioned anywhere else. Thanks man!Weapon
Please be aware that calling dialog.getWindow().setSoftInputMode() after dialog.show() on Android 4-8 has a nasty side effect: dialog remains on screen after configuration changes, still tied to already destroyed Activity/Fragment.Incumber
When you have a list, a pager or other view that uses an adapter (which is nesting the edittext) on a dialog, AlertDialog is setting these flags so no soft input will be triggered. Just clear the flags and set the soft input mode.Penult
W
9

Kotlin

Here's the tested code.

val dialog = AlertDialog.Builder(requireContext()).apply {
    setTitle(…)
    setView(editText)
    setPositiveButton(…)
    setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.

Import AlertDialog from androidx.appcompat.app.AlertDialog. Import WindowManager from android.view.

Wagon answered 2/12, 2019 at 15:51 Comment(0)
M
3

Here's my solution, it's working well for dialog.

txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.

android:windowSoftInputMode="stateAlwaysHidden"
Magnus answered 28/8, 2021 at 4:44 Comment(1)
Thanks. The only solution worked for me. In my case I use Dialog and not AlertDialogJampan
I
1

Dialog Fragment With Kotlin

override onStart Method

override fun onStart() {
    super.onStart()
    dialog.window?.
    setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
    )
}

if you want to close after dismiss then override dismiss method with below code

override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
Itu answered 27/7, 2020 at 7:3 Comment(0)
B
0

I have tried and had success with:

editText.requestFocus();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Baiss answered 30/12, 2023 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.