Listener for Done button on EditText?

JavaAndroidKotlin

Java Problem Overview


If I have an EditText and I want to listen for if the user presses the "done" button on the keypad.. how would I do this?

Java Solutions


Solution 1 - Java

Dinash answer is nice, but it is not working on all devices. Below code works fine for all devices

edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
});

Solution 2 - Java

Code is

final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }
});

In that 'edittext' is id of textfield

Check out this link Simply set setOnKeyListener to your editText

Solution 3 - Java

Kotlin Extension Solution

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

edittext.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Call onDone result 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) {
    // These lines optional if you don't want to set in Xml
    imeOptions = EditorInfo.IME_ACTION_DONE
    maxLines = 1
    setOnEditorActionListener { _, actionId, _ ->
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            callback.invoke()
            true
        }
        false
    }
}

> ###Don't forget to add these options to your edittext Xml, if not in code

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

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

Solution 4 - Java

The same Jone answer, but with replaced lambda:

etPointCombatFirst.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            });

Solution 5 - Java

Based on the response of Asad Rao, I created this KOTLIN extension function.

fun TextView.onClickKeyboardDoneButton(funExecute: () -> Unit) {
    this.setOnEditorActionListener { _, actionId, _ ->
        when (actionId) {
            EditorInfo.IME_ACTION_DONE -> {
                funExecute.invoke()
                true
            }
            else -> false
        }
    }
}

Use:

myEditText.onClickKeyboardDoneButton{myFunctionToExecuteWhenUserClickDone()}

Solution 6 - Java

This Kotlin version should work on all devices:

editText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do something
        true
    } else {
        false
    }
}

Solution 7 - Java

Rx way to do this:

fun EditText.onImeActionDoneClicks(): Observable<Unit> {
    return Observable.create { emitter ->
        setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                emitter.onNext(Unit)
                true
            } else {
                false
            }
        }
    }
}

compositeDisposable += lastEditText.onImeActionDoneClicks().subscribe {
    toast("onImeDoneClicks")
}

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
QuestionSkizitView Question on Stackoverflow
Solution 1 - JavaAsad RaoView Answer on Stackoverflow
Solution 2 - JavaDinashView Answer on Stackoverflow
Solution 3 - JavaGiboltView Answer on Stackoverflow
Solution 4 - JavaSerg BurlakaView Answer on Stackoverflow
Solution 5 - JavaEmmanuel GutherView Answer on Stackoverflow
Solution 6 - JavaWesty92View Answer on Stackoverflow
Solution 7 - Javali2View Answer on Stackoverflow