49

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); } }); 
4
  • 1
    You can find a solution here: workingfromhere.com/blog/2011/04/27/… OR here: stackoverflow.com/questions/1109022/… Commented Jul 25, 2012 at 8: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 :( Commented Jul 25, 2012 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() Commented Jul 26, 2012 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. Commented Jul 26, 2012 at 13:40

9 Answers 9

125

In Manifest xml

android:windowSoftInputMode="stateAlwaysHidden" 

It will automatically hide soft keyboard on Dismiss of Dialog

Sign up to request clarification or add additional context in comments.

5 Comments

This should be the accepted answer! It is a bad practice to override properties declared in AndroidManifest.xml
@Renjith can you please elaborate why its bad
it didn't work for me, but edittext hiding (edittext inside dialog) and dialog.dismiss did the job
@SeshuVinay, I'm just a humble engineer, but if times call me, I becoming Batman :) Take care mate ! :)
Wow there's 2 hours of my life gone thank you Android :D
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); 

4 Comments

this is working correct when trying to hide from a DialogFragment
But it leaves a white shadow temporarily, how to prevent the white shadow?
what is 'imm' ??
@BrenddonAnjos InputMethodManager object
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); } } 

1 Comment

why using postDelayed?
7

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.

2 Comments

I haven't EditText. It's dialog in my SettingsActivity. When it's closed keyboard shows :(
Worked. But had to add a number for "hideFlags:". So "...(token, showFlags: 0, hideFlags:0);"
6

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 Comments

It worked finally after trying too many solutions.Nice thing is it doesn't need any view and check its focus.
however, you should check for focus existence, otherwise if it is hidden, the keyboard shows up
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(); } 

1 Comment

why using handler?
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.

Comments

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) } 

Comments

0

The native Android UI has many pitfalls which require workaround / hacks.

For example, when I display two dialogs (d1, d2), in d2 show an EditText and bring up the input method. If I click outside, I'll find that the second dialog d2 closes and the we return to the first dialog d1, but the keyboard doesn't disappear. Instead, it hides behind the first dialog. (Very strange!)

For this bug, none of the above answers worked ( under onDismiss ). I've tried : d1...hideSoftInputFromWindow; d2...hideSoftInputFromWindow respectively, and also attempted using postDelayed.., but nothing worked. Let alone using toggleSoftInput, which was even more unstable.

However, calling hideSoftInputFromWindow before onDismiss does work. Therefore, the problem with the dialog and the keyboard can be permanently solved with this DIRTY approach:

d.setOnDismissListener(dialogInterface -> { View f = d.getCurrentFocus(); if (f instanceof EditText) { d.show(); // dirty! f.clearFocus(); InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(f.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); f.post(d::hide);// dirty! } }); 

I've also tested on different devices with various input methods and am certain that this is a bug. (Not to mention I've seen this BUG a long time ago. )

In conclusion, Android UI is a somewhat outdated technology. Minor old bugs are no longer fixed, but the system is mature enough that these issues can be addressed using workarounds.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.