Categories


Authors

Close / Hide the Android soft keyboard

Close / Hide the Android soft keyboard

a.k.a. “How to make that bloody keyboard go away” The android keyboard can be a bit of a p.i.t.a. for developers who are trying to create a good user experience. Sometimes it won’t open when you would expect it to, and other times it will never dismiss without the user having to hit the back button (something which many android users wont realize). I’ve struggled often with the later situation.

Scouring the internet for advice turned up multiple solutions, and I’ve managed to figure some of my own as well. Here are the best solutions I have found.

Solution 1) Set the inputType to “text”

The easiest way to do this is in the xml layout declaration:

This can also be done programmatically via. the method setInputType() (inherited from TextView). The programmatic equivalent of the xml above is setting the input type to TYPE_CLASS_TEXT and TYPE_TEXT_VARIATION_NORMAL (see below):

This method is slick and seems like the most appropriate and intended way for developers to ensure the keyboard closes when the user hits the action key. Unfortunately, sometimes a situation will arise where this won’t function as intended. When this occurs we’ll need to pull out some bigger guns.

Solution 2) Use the InputMethodManager.hideSoftInputFromWindow()

This method is mentioned frequently on the internet such as on this stackoverflow post here:

You can think of the InputMethodManager as a system service that mediates between your application and any input method you might be trying to use (in this case the keyboard).

A slight modification to this method worked for me in the past as well. Passing the InputMethodManager.HIDE_NOT_ALWAYS flag instead of 0 closed the keyboard for me when the above options had all failed.

If you'd like the code from this example I've shared a sample project demoing each of these solutions over on github: https://github.com/cephus/Android-soft-keyboard

LFMM - Implementing Google Sign In on Android

LFMM - Implementing Google Sign In on Android

Jalaga

Jalaga