Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: catching action for keyboard enter button being pressed

I am trying to respond to pressing the enter button on a standard system on screen soft keybord, in numeric mode. keyboard appears on the screen when myCustomEditText class is given focus.

tried 2 approaches:

  1. overriding onKeyDown():


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            //do sth..
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

  1. setting listener setOnKeyListener:

    myCustomEditText.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                onRightButtonClicked();
                return true;
            }
            return false;
        }
    });

(also tried onKeyDown() in myCustomEditText as well, but with no effect)

problem is: these aproaches above works for all the keys being pressed but for the enter key - the methods are not even being fired at all. (it's not a problem of bad KeyEvent.KEYCODE_...)

my question is: why these methods are not being fired for enter key, but are for other keys, and how to listen to enter key being pressed properly?

like image 662
Samuel Urbanowicz Avatar asked Aug 05 '14 10:08

Samuel Urbanowicz


People also ask

How do I press Enter on Android?

press the shift key and see the change in the icon for the smiley. It changes to the enter key. This works on Google Keyboard in KitKat 4.4. 4.

How do you enter on a mobile keyboard?

Go to your Phone Settings. Find and tap Languages and input. Tap on current keyboard under Keyboard & input methods. Tap on choose keyboards.

How do I change the Done button on my Android keyboard?

First you need to set the android:imeOptions attribute equal to actionDone for your target EditText as seen below. That will change your 'RETURN' button in your EditText's soft keyboard to a 'DONE' button.


1 Answers

You respond to ENTER differently than normal keys, try something like this:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        switch (actionId) {

            case EditorInfo.IME_ACTION_DONE:
                ...
                return true;

            default:
                return false;
        }
    }
});

You can specify which action is performed by ENTER by specifiying android:imeOptions in XML or by calling editText.setImeOptions(...) with one of the constants in EditorInfo. imeOptions also changes how the ENTER key looks! For example if you set imeOptions to actionSearch then the ENTER key will look like a search button.

You can find a list of all possible values for imeOptions along with a description here.

like image 186
Xaver Kapeller Avatar answered Oct 21 '22 10:10

Xaver Kapeller