how do I catch specific key events from the soft keyboard? specifically I'm interested in the "Done" key.
11 Answers
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;
}
}
});
-
1For JellyBean and higher OnEditorActionListener is needed for this use case instead of 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 – Jason Axelson Oct 10 '13 at 21:05
-
1@JasonAxelson 's answer was the best – Ali Bdeir Jul 11 '16 at 15:29
-
This line >> if (actionId & EditorInfo.IME_MASK_ACTION) << isn't valid Java expression, since what's in the brackets is not a boolean value. It won't even compile and I wonder how did this gain so much upvotes. – Oleksandr Berdnikov Aug 06 '16 at 19:07
-
Both `OnEditorActionListener` and `OnKeyListener` ways worked for me. – code_dredd Jul 11 '17 at 10:26
@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

- 4,485
- 4
- 48
- 56
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.

- 4,151
- 10
- 57
- 117

- 8,134
- 3
- 49
- 62
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;
}
});

- 10,316
- 6
- 58
- 76
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;
}
});

- 134
- 1
- 5
-
how can i find keyCode for each and every key pressed from soft keyboard ?? – Bhavana Vadodariya Mar 11 '13 at 08:09
-
@BhavanaVadodariya you can't. At least not on Jelly Bean. See: http://developer.android.com/reference/android/view/KeyEvent.html – Jason Axelson Oct 10 '13 at 21:06
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;
}

- 39
- 1
-
Exactly what I was looking for ;-) Works well even in every `EditText`! Thank you :-) – maresmar Sep 09 '17 at 23:42
-
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;
}
});

- 457
- 4
- 7
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;
}
});

- 1,859
- 20
- 23
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
}
}

- 613
- 6
- 22
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" />

- 91
- 1
- 4
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)
...

- 329
- 5
- 8