Is it possible to detect when the Done key of onScreen keyboard was pressed ?
Asked
Active
Viewed 5.7k times
3 Answers
294
Yes, it is possible:
editText = (EditText) findViewById(R.id.edit_text);
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;
}
});
Note that you will have to import the following libraries:
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

Michael Yaworski
- 13,410
- 19
- 69
- 97

Szabolcs Berecz
- 4,081
- 1
- 22
- 21
-
1Thank you, you gave me a great starting point. Just I needed to use `EditorInfo.IME_ACTION_SEARCH` for the lens search-button instead. – Niki Romagnoli Aug 18 '15 at 16:27
-
Hi, Is this possible with ButterKnife?@mikeyaworski@SzabolcsBerecz – Maulik Dodia Jun 09 '17 at 05:56
-
1@MaulikDodia You can use butterknife's @OnEditorAction() – Ridcully Feb 01 '18 at 08:20
-
Remember to add `android:imeOptions:actionDone` to make this work in your XML layout if using a `TextInputEditText` – gtxtreme Feb 01 '22 at 13:57
3
An Editor Info is most useful class when you have to deal with any type of user input in your Android application. For e.g. in login/registration/search operations we can use it for more accurate keyboard input. An editor info class describes several attributes for text editing object that an input method will be directly communicating with edit text contents.
You can try with IME_ACTION_DONE .
This action performs a Done operation for nothing to input and the IME
will be closed.
Using setOnEditorActionListener
EditTextObj.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_DONE) {
/* Write your logic here that will be executed when user taps next button */
handled = true;
}
return handled;
}
});

Paula
- 59
- 2
- 6

IntelliJ Amiya
- 74,896
- 15
- 165
- 198
1
Using Butterknife you can do this
@OnEditorAction(R.id.signInPasswordText)
boolean onEditorAction(TextView v, int actionId, KeyEvent event){
if (actionId == EditorInfo.IME_ACTION_DONE || event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
/* Write your logic here that will be executed when user taps next button */
}
return false;
}

Zayin Krige
- 3,229
- 1
- 35
- 34