53

I am currently showing softkeyboard using the following code

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

And Here I d'not bind the softkeyboard with Edittext because of that I had used the above code.

Now I want to close the SoftKeyboard so i am currently using the below code but it is not working.

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

Can Anyone suggest me what to use for closing the softKeyboard ?


Based on Below Answer I want to let you clear that I am not using EditText, I use Layout on which I want to show Keyboard and Hide keyboard. I want to send keyboard key event to remote area bcoz of that I didnot used editText.

Cœur
39k25 gold badges206 silver badges282 bronze badges
asked Jan 9, 2012 at 7:12
5
  • 3
    You code for hiding keyboard is same as code for showing the keyboard. Commented Jan 9, 2012 at 7:20
  • yes but i donot know what to use in place of that one.. Commented Jan 9, 2012 at 7:21
  • Check this thread. Commented Jan 9, 2012 at 7:29
  • this doesn't working for me..plz read above edits Commented Jan 9, 2012 at 8:03
  • 13
    /* To Hide Soft Keyboard IN ONE LINE */ ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(),0); Commented Jul 27, 2013 at 11:39

17 Answers 17

97

I have tested and this is working:

...
//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 not right, please have a look at here.

answered Jan 9, 2012 at 8:14
Sign up to request clarification or add additional context in comments.

3 Comments

Why do you use same code line for showing and hiding?
According to stackoverflow.com/q/3568919/3990767 and stackoverflow.com/q/4745988/3990767, it is quite difficult to check if the keyboard is shown, so toggling might not be a good idea if you don't know if it is already shown.
The problem with this approach is, if the user has hidden the keyboard, the second call will show up the keyboard again
41
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
answered Jan 9, 2012 at 7:29

1 Comment

@Mak Then use: getCurrentFocus().getWindowToken()
34

Use this working code :

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
andrewsi
10.7k132 gold badges37 silver badges52 bronze badges
answered Oct 3, 2012 at 7:25

1 Comment

it works, thanks. I want to hide the keyboard when user click Login button, in this case, this solution works better than imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0). because in landscape phone, the keyboard cover the Login button, and user have to hide the keyboard manually.
9

If you want, you can use entire class and call KeyboardUtil.hideKeyBoard(context) method 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);
 }
 }
}
gnat
6,240122 gold badges57 silver badges76 bronze badges
answered Oct 3, 2012 at 7:29

2 Comments

A solution that always works and sends flag "HIDE_NOT_ALWAYS", funny :)
This works well. There is no need of catching Exception though - catching Exception looks ugly.
5

Close/hide the Android Soft Keyboard

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

Open the Android Soft Keyboard

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 if (inputMethodManager != null) {
 inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
 }
answered Sep 16, 2017 at 7:48

Comments

2

user942821's answer for hiding it works:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

But this also works for me to hide it:

imm.toggleSoftInput(0, 0);

You might also want to try:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

When using '0' in the first parameter sometimes the keyboard toggles on in the wrong places under weird circumstances that I haven't been able to figure out how to duplicate yet. I'm still testing this last example, but will update when I find out more.

See toggleSoftInput documentation page for more information.

answered Dec 17, 2013 at 22:52

4 Comments

Well, InputMethodManager.SHOW_FORCED is just an enum which represent an int value. So of course it works, but that would be the same as if you set it to InputMethodManager.RESULT_UNCHANGED_SHOWN which holds the value 0. Enum should be always used where it needs to be. See developer.android.com/reference/android/view/inputmethod/…
Sure, I understand, but shouldn't that go in the 2nd parameter? The first parameter is for showFlags. public void toggleSoftInput (int showFlags, int hideFlags) I think this is more appropriate: imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, InputMethodManager.RESULT_UNCHANGED_SHOWN);
Good point! :) Actually, there is no Enum Flag which supports the 0 value in the ShowFlags parameter. By reading the doc again I realized that they specify it can be 0. Sorry, my bad, although I don't find it a good practice not having provided a default Enum flag.
I've been looking into it as well, seems like you're supposed to use magic numbers here. I added a link to the docs.
2

This works fine

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
answered May 22, 2015 at 10:55

2 Comments

An explanation would be nice.
Doesn't work in all cases. I'm finding out that this is a bigger problem then it first seems to capture all the edge cases.
0

You could also try

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
answered Jan 9, 2012 at 7:44

Comments

0

Here is s solution, which checks if 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);
 }
answered Aug 24, 2016 at 23:01

Comments

0
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Munawir
3,3569 gold badges36 silver badges51 bronze badges
answered Sep 20, 2016 at 5:55

Comments

0

For hiding keyboard,

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

Here, "mView" can be any view which is visible on screen

answered Oct 22, 2016 at 11:56

Comments

0

This code hides the keyboard from inside onItemClick of an 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);
}
answered Dec 30, 2016 at 18:58

Comments

0

Use a Kotlin Extension to hide keyboard

// Simple, reuseable call anywhere in code
myView.hideKeyboard()
fun View.hideKeyboard() {
 val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
 inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

How to show soft keyboard

fun View.showKeyboard() {
 val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
 inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}

Related further topics:

Simplify adding Done/Next... action to edittext: read this post

Remove requirement for ever using getSystemService: Splitties Library

answered Jan 17, 2020 at 3:09

Comments

0
 public void hideKeyboard(Activity activity) {
 InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
 //Find the currently focused view, so we can grab the correct window token from it.
 View view = activity.getCurrentFocus();
 //If no view currently has focus, create a new one, just so we can grab a window token from it
 if (view == null) {
 view = new View(activity);
 }
 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
 imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

and Call where you want

 hideKeyboard(MainActivity.this);
answered Apr 14, 2021 at 9:10

Comments

0

To hide the keyboard programmatically::

fun hideKeypad() {
 val view = currentFocus
 if (view != null) {
 val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
 imm.hideSoftInputFromWindow(view.windowToken, 0)
 }
}
answered Aug 19, 2021 at 11:37

Comments

0

Kotlin way Hide and Show the Android Soft Keyboard:

Create a Extension file in kotlin and add below code for hide/show the Keyboard.

> fun EditText.showKeyboard(){
 requestFocus()
 val insetsController = ViewCompat.getWindowInsetsController(this)
 insetsController?.show(WindowInsetsCompat.Type.ime())
}
fun Activity.hideKeyboard(){
 val insetsController = ViewCompat.getWindowInsetsController(window.decorView)
 insetsController?.hide(WindowInsetsCompat.Type.ime())
}
answered Sep 11, 2021 at 19:32

Comments

-4
private void close() {
 this.requestHideSelf(0);
}

this method is very simple

answered Mar 29, 2015 at 19:57

1 Comment

Uhh, where are you getting this method from?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.