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?

6 Answers 6

16

Try this in 3rd line of your code:

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

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

Comments

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.

2 Comments

:-/ 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.
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.
4

You can also try

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0 );

Comments

4
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

Comments

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

1 Comment

Is it also useful for hidding the softkey in a webview ALWAYS?
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) 

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.