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:

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

EditText editText = (EditText) findViewById(;
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).

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.

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:

    • Vinod supnekar
    • February 4th, 2016

    Thank you boss …last line done trick…

  1. Multimedia and Design – DVD Slideshow Builder Weave Photos, Video and Music into DVD Slideshow Builder. The ultimate software to weave your photos and video clips into a stunning DVD slideshow and share with your friends.

    • Roland Gabriel
    • March 4th, 2015

    Thank you, mate! Help me out to finish struggle with a bug!

  2. Thank you saved my day!

    • joe w
    • December 20th, 2014

    This is the only thing i have found for a pesky dialog of mine that works. It was driving me nuts!!! thanks!

    • 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