How to catch a "Done" key press from the soft keyboard

Android

Android Problem Overview


how do I catch specific key events from the soft keyboard? specifically I'm interested in the "Done" key.

Android Solutions


Solution 1 - Android

I am not quite sure which kind of listener was used in the accepted answer. I used the OnKeyListener attached to an EditText and it wasn't able to catch next nor done.

However, using OnEditorActionListener worked and it also allowed me to differentiate between them by comparing the action value with defined constants EditorInfo.IME_ACTION_NEXT and EditorInfo.IME_ACTION_DONE.

editText.setOnEditorActionListener(new OnEditorActionListener() {
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
			doSomething();
			return true;
		}
		else {
			return false;
		}
	}
});

Solution 2 - Android

@Swato's answer wasn't complete for me (and doesn't compile!) so I'm showing how to do the comparison against the DONE and NEXT actions.

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        int result = actionId & EditorInfo.IME_MASK_ACTION;
        switch(result) {
        case EditorInfo.IME_ACTION_DONE:
            // done stuff
            break;
        case EditorInfo.IME_ACTION_NEXT:
            // next stuff
            break;
        }
    }
});

Also I want to point out that for JellyBean and higher OnEditorActionListener is needed to listen for 'enter' or 'next' and you cannot use OnKeyListener. From the docs:

> As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged. You should never rely on receiving KeyEvents for any key on a soft input method.

Reference: http://developer.android.com/reference/android/view/KeyEvent.html

Solution 3 - Android

Note: This answer is old and no longer works. See the answers below.

You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

Find the docs here.

Solution 4 - Android

just do like this :

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_DONE)
    {
       //Do Something
    }
     
    return false;
}
});

Solution 5 - Android

    etSearchFriends = (EditText) findViewById(R.id.etSearchConn);
	etSearchFriends.setOnKeyListener(new OnKeyListener() {
	    public boolean onKey(View v, int keyCode, KeyEvent event) {
	        // If the event is a key-down event on the "enter" button
	        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
	            (keyCode == KeyEvent.KEYCODE_ENTER)) {
	        	Toast.makeText(ACTIVITY_NAME.this, etSearchFriends.getText(),Toast.LENGTH_SHORT).show();
	          return true;
	        }
	        return false;
	    }

	}); 

Solution 6 - Android

To catch a “Done” key press from the soft keyboard override Activity's onKeyUp method. Setting a OnKeyListener listener for a view won't work because key presses in software input methods will generally not trigger the methods of this listener, this callback is invoked when a hardware key is pressed in the view.

// Called when a key was released and not handled by any of the views inside of the activity. 
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
        	// code here
	     	break;
        default:
            return super.onKeyUp(keyCode, event);
    }
    return true;
}

Solution 7 - Android

Note : inputtype mention in your edittext.

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >

edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });

Solution 8 - Android

you can override done key event by this method:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});

Solution 9 - Android

KOTLIN version:

<EditText android:id="@+id/edit_text" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text" />

Do not forget to set android:inputType.

// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)

editText.setOnEditorActionListener { _, actionId: Int, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
	    // Do your logic here.
	    true
    } else {
	    false
    }
}

Solution 10 - Android

I have EditText that searches names, and it automatically shows results below in ListView. SoftInput keyboard only showed "next" button and enter sign - which didn't do anything. I wanted only Done button (no next or enter sign) and also I wanted it when it was pressed, it should close keyboard because user should see results below it.

Solution that I found /by Mr Cyril Mottier on his blog/ was very simple and it worked without any additional code: in xml where EditText is located, this should be written: android:imeOptions="actionDone"

so hidding keyboard with Done button, EditText should look like this:

<EditText
        android:id="@+id/editText1central"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView2tie"
        android:ems="10"
        android:imeOptions="actionDone"
        android:hint="@string/trazi"
        android:inputType="textPersonName" />

Solution 11 - Android

IME_MASK_ACTION is 255, while the received actionId is 6, and my compiler does not accept

if (actionId & EditorInfo.IME_MASK_ACTION) 

which is an int. What is the use of &-ing 255 anyway? So the test simply can be

public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE)
    ...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionoriharelView Question on Stackoverflow
Solution 1 - AndroidSwatoView Answer on Stackoverflow
Solution 2 - AndroidJason AxelsonView Answer on Stackoverflow
Solution 3 - AndroidMia ClarkeView Answer on Stackoverflow
Solution 4 - AndroidArash GMView Answer on Stackoverflow
Solution 5 - AndroidSiraj UddinView Answer on Stackoverflow
Solution 6 - AndroidYamil GarciaView Answer on Stackoverflow
Solution 7 - AndroidHari krishna Andhra PradeshView Answer on Stackoverflow
Solution 8 - AndroidMuhammad Farhan HabibView Answer on Stackoverflow
Solution 9 - AndroidI.StepView Answer on Stackoverflow
Solution 10 - AndroidRanko1977View Answer on Stackoverflow
Solution 11 - Androiduser1712200View Answer on Stackoverflow