<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

How to Close/Hide the Android Soft Keyboard

Published on
5,154 Points
2,154 Views
Last Modified:
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.
0
Comment
0 Comments

Featured Post

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Join & Write a Comment

When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month