android: onKeyListener no se activa para la tecla ENTER presionada para EditText multilínea

I've found a lot of similar questions but all of them don't work for multilines EditText. If i set inputType="text" EditText becomes singleline and returns are just not shown. I also need "Enter" button to be "Enter" button (not "Next" or "Done").

content.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN &&
                keyCode == KeyEvent.KEYCODE_ENTER)

            return false;

preguntado el 27 de noviembre de 13 a las 05:11

2 Respuestas

I don't know why it is not invoking ENTER key event. but you can go for another method which serves you same purpose. use OnEditorActionListener.

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return true;

this method is invoked when you press ENTER key on EditText.

respondido 27 nov., 13:06

for that, observe the text entered in EditText by using TextWatcher (… and search for the character '\n' in text. - Gopal Gopi

este does not work for multilines EditText though does work for singleline (just checked for it) - 4ntoine

for multilined EditText the listener is not fired at all on Enter press - 4ntoine

you must be joking) sure i removed onKeyListener - 4ntoine

i have also digged alot of time on internet for this question's solution then,i found this:

et.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));

et is you edit text.

this,worked like charm for me.

respondido 27 nov., 13:06

does not work for me unfortunately. Does inputType makes sense? - 4ntoine

no,it works for me and i don't this that input type effect's that much, because i have use inputtype as android:inputType="none" - Hamad

oh, i catched it. dispatchKeyEvent() simulates user Enter press, but when the user presses Enter type the event is still not fired. I need to make onKeyListener fired when the user presses Enter. - 4ntoine

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.