How to Close/Hide the Android Soft Keyboard

Nikki PetkopoulosStudent Writer
Android supports both an on-screen keyboard — soft keyboard — and an attached hardware keyboard. Generally, the soft keyboard is supposed to appear when there is a text field, and disappears when the field is out of focus.

However, Android offers you an API that is full of overridable functions, all of which can pose as annoying problems for a mobile developer.

It has often been stated that Android's soft keyboard has a terrible API. A simple Google search proves it (check it out for yourself). But I’m not here to apologize on behalf of Google; I'm here to help you. So without further ado, let's begin.

Problem: you want to hide the keyboard. Unfortunately, you can’t simply type Keyboard.hide(). In order to hide the keyboard, you have to use InputMethodManager. But to be able to use the IMM, it's required to have a Context. This can be an issue if you're trying to hide your keyboard from a static class: what are you going to do if there is no need for any Context? Additionally, the IMM requires users to specify the View or Window the keyboard should hide from, making this more complicated. Hey, I don’t work in Mountain View, so I haven’t the faintest idea why this is the way it is.

While this may seem complex, there is a static utility method that can help; keep in mind it has to be provided by an Activity -- it will not work otherwise.

public static void hide_keyboard(Activity activity) {
                          InputMethodManager inputMethodManager = (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);
                          inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

Open in new window

If the keyboard is showing from an EditText hosted in a DialogFragment, use this method instead:
hide_keyboard(get_activity()); //won't work
                      This won't work because you'll be passing a reference to the Fragment's host Activity, which will have no focused control while the Fragment is shown! Wow! So, for hiding the keyboard from Fragments, I must resort to the lower-level, more common, and uglier method.

Open in new window

To hide the keyboard from Fragments, the method below, albeit lower-tier, is appropriate:
public static void hide_keyboard_from(Context context, View view) {
                          InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
                          inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

Open in new window

Still not feeling satisfied?

Here's yet another solution that's quicker and great for flipping between Fragments:
private void hideKeyboard() {   
                          // Check if no view has focus:
                          View view = this.getCurrentFocus();
                          if (view != null) {
                              InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                              inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Open in new window

Note: this is the Activity.

I hope you all found this helpful. Good luck hiding that darned keyboard.
Nikki PetkopoulosStudent Writer

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.