Android Use Done button on Keyboard to click button Android Use Done button on Keyboard to click button android android

Android Use Done button on Keyboard to click button


You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:

max.setOnEditorActionListener(new OnEditorActionListener() {        @Override        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {                Log.i(TAG,"Enter pressed");            }                return false;        }    });


You can try with IME_ACTION_DONE .

This action performs a “done” operation for nothing to input and the IME will be closed.

 Your_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;            }        });


Kotlin Solution

The base way to handle the done action in Kotlin is:

edittext.setOnEditorActionListener { _, actionId, _ ->    if (actionId == EditorInfo.IME_ACTION_DONE) {        // Call your code here        true    }    false}

Kotlin Extension

Use this to call edittext.onDone {/*action*/} in your main code. Keeps it more readable and maintainable

edittext.onDone { submitForm() }fun EditText.onDone(callback: () -> Unit) {    setOnEditorActionListener { _, actionId, _ ->        if (actionId == EditorInfo.IME_ACTION_DONE) {            callback.invoke()            true        }        false    }}

Don't forget to add these options to your edittext

<EditText ...    android:imeOptions="actionDone"    android:inputType="text"/>

If you need inputType="textMultiLine" support, read this post