Hide soft keyboard after dialog dismiss
36,324
Solution 1
In Manifest xml
android:windowSoftInputMode="stateAlwaysHidden"
It will automatically hide soft keyboard on Dismiss of
Dialog
Solution 2
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);
Solution 3
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);
}
}
Solution 4
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.
Solution 5
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);
Related videos on Youtube
Author by
Dmytro Zarezenko
CTO at ElephantsLab, Founder/CEO of Asymptix, Web developer, Blockchain developer IT Guru Homepage Asymptix LinkedIn Twitter
Updated on July 09, 2022Comments
-
Dmytro Zarezenko almost 2 years
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); } });
-
AndroidGuy almost 12 yearsYou can find a solution here: workingfromhere.com/blog/2011/04/27/… OR here: stackoverflow.com/questions/1109022/…
-
Dmytro Zarezenko almost 12 yearsBoth 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 :(
-
AndroidGuy almost 12 yearsif 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 almost 12 yearsor 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.
-
-
Dmytro Zarezenko almost 12 yearsI haven't EditText. It's dialog in my SettingsActivity. When it's closed keyboard shows :(
-
Renjith almost 9 yearsThis should be the accepted answer! It is a bad practice to override properties declared in AndroidManifest.xml
-
Rajeshwar over 8 years@Renjith can you please elaborate why its bad
-
narancs over 8 yearsit didn't work for me, but edittext hiding (edittext inside dialog) and dialog.dismiss did the job
-
narancs about 8 years@SeshuVinay, I'm just a humble engineer, but if times call me, I becoming Batman :) Take care mate ! :)
-
Kammaar over 7 yearsthis is working correct when trying to hide from a DialogFragment
-
Sreekanth Karumanaghat about 6 yearsBut it leaves a white shadow temporarily, how to prevent the white shadow?
-
Brenddon Anjos over 5 yearswhat is 'imm' ??
-
Daniel Wilson over 5 yearsWow there's 2 hours of my life gone thank you Android :D
-
Sam Chen over 4 years@BrenddonAnjos
InputMethodManager
object -
AJW over 4 yearsWorked. But had to add a number for "hideFlags:". So "...(token, showFlags: 0, hideFlags:0);"
-
Ravi Yadav over 4 yearsIt worked finally after trying too many solutions.Nice thing is it doesn't need any view and check its focus.
-
Madina Saidova about 4 yearshowever, you should check for focus existence, otherwise if it is hidden, the keyboard shows up
-
zihadrizkyef over 2 yearswhy using handler?
-
zihadrizkyef over 2 yearswhy using postDelayed?