when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop
Asked Answered
B

14

126

I am using AlertDialog.Builder in order to create an input box, with EditText as the input method.

Unfortunately, the Soft Keyboard doesn't pop, although the EditText is in focus, unless you explicitly touch it again.

Is there a way to force it to pop?

I've tried the following, after the (AlertDialog.Builder).show(); but for no avail.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

Anyone can help?

Thanks!!

Beyer answered 11/8, 2010 at 4:14 Comment(2)
please format your source code.Knucklebone
Then I upvote you as well :) I had the same problem search for multiple hours and the last answer from grine4ka works greatKnucklebone
A
232

I've made such a thing

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();
Aryn answered 20/7, 2011 at 9:0 Comment(11)
Thank you so much. I've searched for a while now and this is the way you want to go. All the OnFocusChangeListener approaches seem to much to me and cause trouble. You have to create the AlertDialog from the AlertDialog.Builder!Knucklebone
Is this one really a solution? This just forces the keyboard to show, regardless if there is an input field or not, regardsless if the input field has focus or not, right? =)Babarababassu
@Babarababassu you're right this one is not the real solution, but it works.i've tried to do such thing if there is no edittext in dialog, and soft keyboard didn't appear.Aryn
I actually manage to "solve it" (workaround). I use the setOnFocusChangeListener for the EditText, and in onFocusChange if check if it has focus (the "hasFocus" var) and if so, I do getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);Babarababassu
SOFT_INPUT_STATE_UNCHANGED also works if your keyboard is already visible before dialogue pops up.Ginnygino
oh ya. thanks, i appreciate it enough to write 15 extra chars to make this comment validate..Electroanalysis
I had the opposite requirement. The keyboard should NOT popup. I used the same solution, but with LayoutParams.SOFT_INPUT_STATE_HIDDEN. Thanks.Irisation
Why didnt mark this answer as correct? It's works for me thank, you save my time.Cemetery
Note: For this to work, you need to place the setSoftInputMode line before dialog.show() or it won't work. +1 for the simple correct solution btwDorcia
if it will not work, add line ' edittext.requestFocus()', it works for meAcetaldehyde
just a mention to use dialog.show() instead of builder.show() otherwise it doesn't workContinuity
A
31

I've managed to solve it like this:

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Altair answered 6/6, 2013 at 21:40 Comment(1)
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.Desireedesiri
J
25

I found out that the same code works properly on Tablet, the keyboard does pop up, but on Phone it doesn't, so researching further, seems to point to the "adjust" option.

I am using this, feels much cleaner.

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();
Java answered 20/5, 2014 at 14:16 Comment(1)
Thanks. This is must better than using SOFT_INPUT_STATE_ALWAYS_VISIBLE. As SOFT_INPUT_STATE_ALWAYS_VISIBLE is going to block the UI components of dialog, where SOFT_INPUT_ADJUST_RESIZE able to resize and "push up" the dialog.Fearless
K
14

In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment:

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

Note the SOFT_INPUT_STATE_ALWAYS_VISIBLE instead of SOFT_INPUT_STATE_VISIBLE.

From documentation:

// Visibility state for softInputMode: please always make the soft input 
// area visible when this window receives input focus.
int SOFT_INPUT_STATE_ALWAYS_VISIBLE;
Kape answered 15/9, 2017 at 22:16 Comment(2)
This was the only solution that worked for me and I had tried LOADS of them. Mine was a dialogfragment build from alertdialog builder. The important bit seemed to be placing the above code in the onResume(). Anywhere else it just didn't work!Cown
This solution works very well. The solution is for a dialog fragment but it worked well with an AlertDialog and I think it works for any kind of dialog. Just put those three line after calling the show() method of AlertDialog, then request focus for your input view.Pulpy
F
7

When you call showDialog() to show a Dialog created using AlertDialog in onCreateDialog()

You should put the code in onPrepareDialog():

@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });
}
Festinate answered 26/1, 2011 at 16:52 Comment(0)
V
7

A much better solution is given here.

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

No workaround. EditText behaves as expected.

Vermiform answered 31/5, 2013 at 15:33 Comment(1)
This one worked for me, the other solution were bringing the focus but keybord was not displayed.Birdt
C
6

In my case, the SoftInputMode wasn't getting displayed when I set it which was before showing the dialog (after creating it). The below code worked for me where I set the SoftInputMode after showing the dialog.

Kotlin:

val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                .create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

Java:

AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                .create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

I hope this helps anyone who was having the same problem as me.

Crusado answered 9/6, 2020 at 7:39 Comment(0)
F
2
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);
Frangos answered 12/5, 2016 at 6:24 Comment(0)
A
1

This was answered here already. Using an OnFocusChangeListener worked for me.

Amain answered 9/9, 2010 at 19:27 Comment(1)
The question asks how to set the soft input mode for an AlertDialog.Builder object, however the thread you refer to gives an example using an AlertDialog object. If I try to use the suggested code (using alert.getWindow().setSoftInputMode(...) within OnFocusChangeListener) Eclipse objects that the method getWindow() is not defined for the type AlertDialog.Builder. Can you help me fix this, please?Fortna
G
1

This problem occurs when EditText is added after AlertDialog.onCreate is called.

https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder

The AlertDialog class takes care of automatically setting android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you based on whether any views in the dialog return true from View.onCheckIsTextEditor().

You need to clear the FLAG_ALT_FOCUSABLE_IM flag.

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 

Because AlertDialog.show is called in the DialogFragment.onStart, you can insert the code in the DialogFragment.onStart.

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

Or you can use the Dialog.setOnShowListener if you do not use a DialogFragment.

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
});
Gawain answered 7/7, 2020 at 2:21 Comment(0)
P
0

Try this, its working for me

    Window window = dialog.getWindow();
    if (window != null) { // After the window is created, get the SoftInputMode
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
Phosphorate answered 11/8, 2010 at 4:14 Comment(0)
T
0

Try this, its working for me

If you want to display soft keyboard:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

And if you want to hide the it:

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
Tobey answered 30/3, 2016 at 11:12 Comment(0)
A
0
final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Antimalarial answered 7/1, 2017 at 21:23 Comment(1)
It's better to include some context/explanation with code as this makes the answer more useful for the OP and for future readers.Umont
K
0

I found an easy and reliable solution to this, just put a hidden EditText on root of your dialog layout if you got a complex layout which an editable field isn't in root,

<!-- Just to trick AlertDialog to not hide soft keyboard -->
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

This is basically to trick this part of compat/androidx.

I used to use onResume solution above but with that I couldn't use simpler API of AlertDialog.Builder() to remove the use of AppCompatDialogFragment but now I can simply use the easier API.

Katy answered 13/5, 2021 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.