How to programmatically close Android Soft KeyBoard?

I am currently showing a soft keyboard using the following code

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN); 

And here I do not associate a soft keyboard with Edittext, because I used the code above.

Now I want to close SoftKeyboard, so I am currently using the code below, but it does not work.

 imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN); 

Can someone suggest me what to use to close softKeyboard?




Based on the following answer, I want you to understand that I am not using EditText, I am using the layout on which I want to show the keyboard and hide the keyboard. I want to send a keyboard keyboard event to a remote bcoz area that I did not use editText.

+43
android android-softkeyboard
Jan 9 '12 at 7:12
source share
13 answers

I tested and it works:

 ... //to show soft keyboard imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); //to hide it, call the method again imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

By the way, the second parameter of your code is incorrect, look here .

+87
Jan 09 '12 at 8:14
source share
β€” -
 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0); 
+36
Jan 09 '12 at 7:29
source share

Use this working code:

 InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
+30
Oct 03 '12 at 7:25
source share

If you want, you can use the entire class and call the KeyboardUtil.hideKeyBoard method (context) wherever:

 public class KeyboardUtil { public static void hideKeyboard(Activity activity) { try { InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } catch (Exception e) { // Ignore exceptions if any Log.e("KeyBoardUtil", e.toString(), e); } } } 
+9
Oct 03 '12 at 7:29
source share

user942821 answer for hiding it:

 imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

But it also helps me hide this:

 imm.toggleSoftInput(0, 0); 

You can also try:

 imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0); 

When using '0' in the first parameter, sometimes the keyboard switches to the wrong places in strange circumstances, which I still could not figure out how to duplicate. I am still testing this last example, but am updating when I learn more.

See the toggleSoftInput documentation page for more information .

+2
Dec 17 '13 at 22:52
source share

It works great

 InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0); 
+2
May 22 '15 at 10:55
source share

Close / Hide Softkey for Android

 View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } it working for me i hope it work for you.. 

Open Android Soft Keyboard

  InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (inputMethodManager != null) { inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); } 
+1
Sep 16 '17 at 7:48
source share

You can also try

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
0
Jan 09 2018-12-12T00:
source share

Here is the s solution that checks if the keyboard is visible

  public static void hideKeyboard(Activity activity) { if (isKeyboardVisible(activity)) { InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); } } public static boolean isKeyboardVisible(Activity activity) { ///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device Rect r = new Rect(); View contentView = activity.findViewById(android.R.id.content); contentView.getWindowVisibleDisplayFrame(r); int screenHeight = contentView.getRootView().getHeight(); int keypadHeight = screenHeight - r.bottom; return (keypadHeight > screenHeight * 0.15); } 
0
Aug 24 '16 at 23:01
source share
 InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
0
Sep 20 '16 at 5:55
source share

To hide the keyboard

 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mView.getWindowToken(), 0); 

Here "mView" can be any view visible on the screen

0
Oct 22 '16 at 11:56
source share

This code hides the keyboard inside onItemClick from AutoCompleteTextView

 public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) { // whatever your code does InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0); } 
0
Dec 30 '16 at 18:58
source share
 private void close() { this.requestHideSelf(0); } 

this method is very simple

-2
Mar 29 '15 at 19:57
source share



All Articles