Perform an Action after Enter keypress on EditText

How to Perform an Action after Enter keypress on EditText, Step-By-Step


1. Add an EditText to your project.  


2. Add the below imports to MainActivity.java file

    import android.view.View.OnKeyListener;
    import android.view.View;
    import android.view.KeyEvent;

3. Add the below code to the onCreate method.

    editText.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View view, int keyCode, KeyEvent keyevent) {
            //If the keyevent is a key-down event on the "enter" button
            if ((keyevent.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                //...
                // Perform your action on key press here
                // ...    
                return true;
            }
            return false;
        }
});
                      
4. Compile and run!