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:

1
2
3
4
5
6
<EditText
	android:id="@+id/my_edit_text"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint="Tap here to type"
	android:inputType="text" />

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):

1
2
EditText editText = (EditText) findViewById(R.id.my_edit_text);
editText.setRawInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);

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).

1
2
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

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.

1
2
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

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

    • Tomas
    • July 25th, 2014

    Thaaaaank youuu !!! It was driving me crazy ! xd…

    • wells
    • June 18th, 2013

    Thank you so much! Amazing tutorial!

    • Tahir Yilmaz
    • January 17th, 2013

    I’m writing with 2.3.3 . I tried everything I could to hide my keyboard from my EditText, including all the examples above, but it simply doesn’t work. Could you help me with this please ?

    • khushboo
    • April 17th, 2012

    thanks a lot!!!!

    • Sonsum
    • April 14th, 2012

    Thank you so much! This was so simple and yet so effective!

  1. October 24th, 2011
    Trackback from : Dvorak Keyboard Android