Android Quick Tip: EditText with DONE Button That Closes the Keyboard

Android Quick Tip

Close the keyboard when you click ‘DONE’… seems like a simple task, right? Unfortunately, clicking ‘DONE’ or the ‘RETURN’ button in even a single line Android EditText will not drop focus and close the soft keyboard. In most cases it will just generate a new line of text. So what do you have to do if you want to close the soft keyboard by clicking ‘DONE’?

First you need to set the android:imeOptions attribute equal to “actionDone” for your target EditText as seen below. That will change your ‘RETURN’ button in your EditText’s soft keyboard to a ‘DONE’ button.

* example_edittext.xml

    android:hint="Enter some text"

Now we need to create a custom OnEditorActionListener for the target EditText that will recognize when the ‘DONE’ button has been clicked. In it we override the onEditorAction() method, get an instance of the InputMethodManager, and use it to close the soft keyboard. Here’s the code for the custom OnEditorActionListener class:


class DoneOnEditorActionListener implements OnEditorActionListener {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            return true;   
        return false;

Now go to the onCreate() method of the Activity that contains the target EditText. We’ll call it SampleActivity here. In here you will assign the new DoneOnEditorActionListener to the target EditText via the setOnEditorActionListener() method.


public class SampleActivity extends Activity {      
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.sample_activity_layout); // sample_activity_layout contains our target EditText, target_edittext
        EditText targetEditText = (EditText)findViewById(; 
        targetEditText.setOnEditorActionListener(new DoneOnEditorActionListener());
        // The rest of the onCreate() code

And there you go, your target EditText field will now close the soft keyboard whenever you click the ‘DONE’ button. Very handy and not too difficult. Enjoy.

11 Responses to “Android Quick Tip: EditText with DONE Button That Closes the Keyboard”

  1. Jill says:

    That huge confusing mess of code… just to make a common/normal “done” mean “done”?


    And if you have 5 editText boxes… it’s even a bigger mess.

  2. Jill says:

    Does that code only work if the keyboard button says DONE?

    How would I do it if it was a SEARCH button?

  3. Bijaya Guin says:

    very Nice Information

  4. Adnan says:

    Thanks, It helped me.

  5. Anthony Tannoury says:

    Very useful! Thank you very much!

  6. Roger Bacon says:

    Maybe it is easier to put an line
    into the edittext tag in the layout xml file …

  7. Iram Bukhari says:

    Thanks for sharing….it helped me.

  8. Ante says:

    Excellent, thanks !

  9. Peter from Honolulu says:

    android:imeOptions=”actionDone” doesn’t work but setting

    sets the keyboard RETURN key to DONE but it will also limit the entry to one singe line of text.

  10. Anuj says:

    Awesome…. works perfect! Thanks a lot :)

  11. Patrick says:

    Thanks this was very helpfull!

    I’ve added v.clearFocus(); to the class to not only hide the keyboard, but also clear the focus :)