Implicit "Submit" after hitting Done on the keyboard at the last EditText

Simple and effective solution with Kotlin

Extend EditText:

fun EditText.onSubmit(func: () -> Unit) {
    setOnEditorActionListener { _, actionId, _ ->

       if (actionId == EditorInfo.IME_ACTION_DONE) {
           func()
       }

       true

    }
}

Then use the new method like this:

editText.onSubmit { submit() }

Where submit() is something like this:

fun submit() {
    // call to api
}

More generic extension

fun EditText.on(actionId: Int, func: () -> Unit) {
    setOnEditorActionListener { _, receivedActionId, _ ->

       if (actionId == receivedActionId) {
           func()
       }

        true
    }
}

And then you can use it to listen to your event:

email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })

Try this:

In your layout put/edit this:

<EditText
    android:id="@+id/search_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:singleLine="true"
    android:imeOptions="actionDone" />

In your activity put this (e. g. in onCreate):

 // your text box
 EditText edit_txt = (EditText) findViewById(R.id.search_edit);

 edit_txt.setOnEditorActionListener(new EditText.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
         if (actionId == EditorInfo.IME_ACTION_DONE) {
             submit_btn.performClick();
             return true;
         }
         return false;
     }
 });

Where submit_btn is your submit button with your onclick handler attached.

Tags:

Android