TextView's setOnKeyListener for enter key is not working

2.9k views Asked by At

I had the following code in onCreate:

commentET = (EditText) findViewById(R.id.comment);
commentET.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
               case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    Log.i("UdazzT", "enter pressed");
                    return true;
                default:
                    break;
            }
        }
        return false;
    }
});

and the layout:

<EditText android:hint="@string/comment"
    ...
    android:imeOptions="actionGo"/>

I've also tried with actionSend and actionSearch

3

There are 3 answers

1
shkschneider On

android:imeOptions="actionGo" correspond to the EditorInfo.IME_ACTION_GO:

    commentET.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(final TextView textView, final int keyCode, final KeyEvent keyEvent) {
            if (keyCode == EditorInfo.IME_ACTION_GO) {
                // ...
                return true;
            }
            return false;
        }

    });

I personaly use:

final Integer[] enterKeys = {
    EditorInfo.IME_ACTION_GO,
    KeyEvent.KEYCODE_DPAD_CENTER,
    KeyEvent.KEYCODE_ENTER
};
if (Arrays.asList(enterKeys).contains(keyCode)) {
    // ...
}
0
Cruceo On

I actually use this static helper method for determining if a KeyEvent is an Enter Action, since some device manufacturers don't like to follow standards (e.g. sometimes the KeyEvent can actually be null, or it could be ACTION_DOWN):

public static boolean isSoftKeyboardFinishedAction(TextView view, int action, KeyEvent event){
    // Some devices return null event on editor actions for Enter Button
    return (action == EditorInfo.IME_ACTION_DONE || action == EditorInfo.IME_ACTION_GO || action == EditorInfo.IME_ACTION_SEND) && (event == null || event.getAction() == KeyEvent.ACTION_DOWN);
}

And then I simply implement that with:

editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        public boolean onEditorAction(TextView view, int action, KeyEvent event) {
            if (isSoftKeyboardFinishedAction(view, action, event)) {
                // Do something
                return true;
            }
            return false;
        }
    }); 

Obviously, shkschneider's answer is a bit cleaner using the a List with contains(), but you need to also take into account the KeyEvent or else the callback will be triggered twice on some devices.

0
AudioBubble On

I found the solution with:

    commentET = (EditText) findViewById(R.id.comment);
    commentET.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                perform action
                return true;
            }
            return false;
        }
    });