Skip to content Skip to sidebar Skip to footer

Listener For Done Button On Edittext?

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?

Solution 1:

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

edittext.setOnEditorActionListener(newTextView.OnEditorActionListener() {
    @OverridepublicbooleanonEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
            returntrue;
        }
        returnfalse;
    }
});

Solution 2:

Code is

finalEditTextedittext= (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(newView.OnKeyListener() {
    publicbooleanonKey(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();
            returntrue;
        }
        returnfalse;
    }
});

In that 'edittext' is id of textfield

Check out this link Simply set setOnKeyListener to your editText

Solution 3:

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 heretrue
    }
    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:

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 5:

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();
                    returntrue;
                }
                returnfalse;
            });

Post a Comment for "Listener For Done Button On Edittext?"