6

I have a tabbed view with one Activity per tab, and when I switch from the first tab, which has a TextView, to the second tab, which only shows a clickable list, the soft keyboard is still there. I want it to go away, so I tried this:

public static void hideSoftKeyboard (Activity activity, View view) {
  InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
  imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

but this does not work, because there is no relevant view to provide, as there is no View on the screen that takes keyboard input. Any suggestions on how to solve this?

Lars D
  • 8,483
  • 7
  • 34
  • 37

6 Answers6

16

Try this in 3rd line of your code:

imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
16

Try the answer provided by Joe on: Stop EditText from gaining focus at Activity startup

Place this inside the manifest for your activity: android:windowSoftInputMode="stateHidden"

This is a common question, and it is great to know that the framework actually handles this very nicely.

Community
  • 1
  • 1
Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
  • 3
    :-/ I have this but it didn't help. When I use the task switcher to switch from an activity with the keyboard up to my activity (which has no text input and so needs no keyboard) the keyboard stays up. – ArtOfWarfare Jan 07 '13 at 04:16
  • 1
    This also doesn't work if you are in one activity, tap an edit text to show the keyboard, and then hit the action bar header's back button. Returning to the previous activity does not hide the keyboard even if it has that windowSoftInputMode setting. –  Apr 02 '14 at 20:48
4

You can also try

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
4
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Baz
  • 36,440
  • 11
  • 68
  • 94
muzaffar
  • 71
  • 1
  • 9
0

This method may help you to hide keyboard any way. This is working fine for me

public void hideKeyboard(Activity activity, View view) {
        if (activity != null ) {

            if(view != null)
            {
                try {
                    InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else
            {
                activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
            }

        }
    }
Hitesh Bhalala
  • 2,872
  • 23
  • 40
0

I had a similar issue when try to hide keyboard while transition animation playing.

This worked for me:

imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0)
pratt
  • 1,534
  • 14
  • 17