46

I want to hide soft keyboard after AlertDialog dismiss, but it's still visible. Here is my code:

alert = new AlertDialog.Builder(MyActivity.this);
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {

    @Override
    public void onDismiss(DialogInterface dialog) {
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
});
Dmytro Zarezenko
  • 10,010
  • 9
  • 52
  • 95
  • 1
    You can find a solution here: http://www.workingfromhere.com/blog/2011/04/27/close-hide-the-android-soft-keyboard/ OR here: https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard – AndroidGuy Jul 25 '12 at 08:54
  • Both methods are use EditText view, but I haven't it. I haven't EditText. It's dialog in my SettingsActivity. When it's closed keyboard shows :( – Dmytro Zarezenko Jul 25 '12 at 12:14
  • if your keyboard popups on a Dialog, it means your dialog must have a EditText within it. So you can write the above code by passing the window-token of your EditText, & then call dialog.dismiss() – AndroidGuy Jul 26 '12 at 13:01
  • or if its not the above case.. means if Edittext is not on your dialog, you need to check for which EditText it popsup & hide it before showing the dialog. If this too doesnt work, plz post your code. – AndroidGuy Jul 26 '12 at 13:40

8 Answers8

109

In Manifest xml

android:windowSoftInputMode="stateAlwaysHidden"

It will automatically hide soft keyboard on Dismiss of Dialog

AskNilesh
  • 58,437
  • 15
  • 99
  • 129
Rajeshwar
  • 1,333
  • 1
  • 11
  • 13
17

I met the same problem. Solved it by doing like this. It doesn't need any reference:

imm.hideSoftInputFromWindow(getWindow().getDecorView()
                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
NordicShaw
  • 203
  • 2
  • 8
8

I had a similar problem when closing an alert dialog. This seems to do the trick for me.

Inside your DialogFragment

public static void closeKB(final View view) 
{
    caller.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }, 1);
}

@Override
public void onDismiss(DialogInterface dialog)
{
    super.onDismiss(dialog);
            View view = getActivity().getCurrentFocus();
    if (view != null)
    {
        closeKB(view);
    }
}
Miika Pakarinen
  • 261
  • 1
  • 4
  • 5
6

I use this method:

IBinder token = searchTextEntry.getWindowToken();
( ( InputMethodManager ) getSystemService( Context.INPUT_METHOD_SERVICE ) ).hideSoftInputFromWindow( token, 0 );

Where searchTextEntry is the name of my EditText reference.

ScouseChris
  • 4,319
  • 29
  • 38
3

This works! This will close the keyboard after dialog dismiss

InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
2
protected void hideKeyboard() {
    final Activity activity = getActivity();
    final View view = activity != null ? activity.getCurrentFocus() : null;
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            if (view != null) {
                InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null)
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }, 1);
}

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    hideKeyboard();
}
Yusuf Yaşar
  • 131
  • 1
  • 4
0

All these advices to use InputMethodManager are somewhat vague - where exactly to call it,
and they do not work at least for me.
Yes, keyboard disappears but then the app crashes!?
The main problem is that hiding of keyboard happens at the same time when dialog is disappearing.

To avoid it dialog.dismiss() should be called in view.postDelayed() after imm.hideSoftInputFromWindow() and in my case I set delay as 150.

ZygD
  • 8,011
  • 21
  • 49
  • 67
0

in case anyone looks for this in kotlin, it would be:

private fun hideDeviceKeyboard() {
    val imm = context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
jvargas
  • 526
  • 4
  • 10