Home > Development > Detect Dismissal of the Soft Keyboard

Detect Dismissal of the Soft Keyboard

When your app has multiple text entry fields that must be checked for validity before the next action happens, it can be important to know that the user dismissed the soft keyboard via the back button. Typically you would want to listen for key and/or touch events from the fields so as to run your validation routine. However, there is not an easy way to get the information about dismissal of the keyboard.

There was a helpful answer posted to Stackoverflow by  Steelight who offered an override of the onKeyPreIme method that you add to your new instance of the EditText class. This is fine if you can do e = new EditText(inflater.getContext()) but not so good if you have to get your EditText instances via e = (EditText) findViewById().

Try creating a subclass of EditText and overriding its onKeyPreIme method. Then you can use this subclass anywhere, especially if you put it in a library project.  If you make the onKeyPreIme method send a key event and your instances of this subclass listen for key events, you can detect that the keyboard was dismissed and do whatever you’d do when any key event is received.

 * This class overrides the onKeyPreIme method to dispatch a key event if the
 * KeyEvent passed to onKeyPreIme has a key code of KeyEvent.KEYCODE_BACK.
 * This allows key event listeners to detect that the soft keyboard was
 * dismissed.
public class ExtendedEditText extends EditText {

	public ExtendedEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);


	public ExtendedEditText(Context context, AttributeSet attrs) {
		super(context, attrs);


	public ExtendedEditText(Context context) {


	public boolean onKeyPreIme(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			return false;
		return super.onKeyPreIme(keyCode, event);


Categories: Development Tags:
  1. Thanh Tuan
    December 19, 2013 at 8:13 am

    Could you please post full code in case “e = new EditText(inflater.getContext())”?. I do know how to implement in this case.

    • December 23, 2013 at 1:34 pm

      Click on the link to the posting at Stackoverflow to see the code.

      • Laurie Taylor
        June 2, 2014 at 3:17 am

        I have tried to implement this code as it is exactly what I need to do. I am an android neophyte having written procedural code since 1970. I have added your code to my app & specified <com.app.entendededittext in my activity_main, but my app abends when it hits the setcontentview. I am obviously missing something, can you advise.

      • June 2, 2014 at 4:11 pm

        I don’t understand what you mean by “specified… in activity_main”. If you created a class called ExtendedEditText and declared instances in a view XML, this should all work without having to specify anything about the new class in your activity.

      • Laurie Taylor
        June 3, 2014 at 1:27 am

        Todd, Thank you for your quick response. After some fiddling I was able to resolve the issue and now have it functioning as I want. Many thanks. Laurie Taylor.

  2. Jasmine
    January 20, 2015 at 7:01 am

    Thank for the post..Helped me !!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: