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);
Share:
36,324

Related videos on Youtube

Dmytro Zarezenko
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, 2022

Comments

  • Dmytro Zarezenko
    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
      AndroidGuy almost 12 years
    • Dmytro Zarezenko
      Dmytro Zarezenko almost 12 years
      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 :(
    • AndroidGuy
      AndroidGuy almost 12 years
      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
      AndroidGuy almost 12 years
      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.
  • Dmytro Zarezenko
    Dmytro Zarezenko almost 12 years
    I haven't EditText. It's dialog in my SettingsActivity. When it's closed keyboard shows :(
  • Renjith
    Renjith almost 9 years
    This should be the accepted answer! It is a bad practice to override properties declared in AndroidManifest.xml
  • Rajeshwar
    Rajeshwar over 8 years
    @Renjith can you please elaborate why its bad
  • narancs
    narancs over 8 years
    it didn't work for me, but edittext hiding (edittext inside dialog) and dialog.dismiss did the job
  • narancs
    narancs about 8 years
    @SeshuVinay, I'm just a humble engineer, but if times call me, I becoming Batman :) Take care mate ! :)
  • Kammaar
    Kammaar over 7 years
    this is working correct when trying to hide from a DialogFragment
  • Sreekanth Karumanaghat
    Sreekanth Karumanaghat about 6 years
    But it leaves a white shadow temporarily, how to prevent the white shadow?
  • Brenddon Anjos
    Brenddon Anjos over 5 years
    what is 'imm' ??
  • Daniel Wilson
    Daniel Wilson over 5 years
    Wow there's 2 hours of my life gone thank you Android :D
  • Sam Chen
    Sam Chen over 4 years
    @BrenddonAnjos InputMethodManager object
  • AJW
    AJW over 4 years
    Worked. But had to add a number for "hideFlags:". So "...(token, showFlags: 0, hideFlags:0);"
  • Ravi Yadav
    Ravi Yadav over 4 years
    It worked finally after trying too many solutions.Nice thing is it doesn't need any view and check its focus.
  • Madina Saidova
    Madina Saidova about 4 years
    however, you should check for focus existence, otherwise if it is hidden, the keyboard shows up
  • zihadrizkyef
    zihadrizkyef over 2 years
    why using handler?
  • zihadrizkyef
    zihadrizkyef over 2 years
    why using postDelayed?