3

I have a EditText field in my activity. The user can change the text in that field using the virtual keyboard. Once the user presses the enter key on the keyboard, I want to perform some action. So, how do I implement a setOnClickListener to the enter button on the keyboard?

Ankush
  • 6,767
  • 8
  • 30
  • 42

5 Answers5

9

use onKeyListener for checking Enter press

for e.g..

edittext.setOnKeyListener(new OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction()!=KeyEvent.ACTION_DOWN)
            return false;
        if(keyCode == KeyEvent.KEYCODE_ENTER ){
            //your necessary codes...
            return true;
        }
        return false;
    }
});

for more information, check the official documentation

Also you can see that example

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
Renjith
  • 5,783
  • 9
  • 31
  • 42
  • thanks this worked perfectly! i only had to change the `editext` to the one i was using in my code, and add a `)` at the end to complete the `(new OnKeyListener() {`, otherwise fantastic. thank you. – Azurespot Jun 17 '14 at 06:04
2

Possible Duplicate to ones below,

Use "ENTER" key on softkeyboard instead of clicking button

Enter Button on Soft KeyBoard - Android

Hope these help

Community
  • 1
  • 1
Royston Pinto
  • 6,681
  • 2
  • 28
  • 46
2

Set a special listener to be called when an action is performed on the text view. This will be called when the enter key is pressed, or when an action supplied to the IME is selected by the user. Setting this means that the normal hard key event will not insert a newline into the text view, even if it is multi-line; holding down the ALT modifier will, however, allow the user to insert a newline character.

edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                    //perform your action
                    //button.performClick();
                }
                return false;
            }
        });
bvanvelsen - ACA Group
  • 1,741
  • 1
  • 14
  • 25
0

I did a little bit of research for you and found this: How to override the <ENTER> key behaviour of the virtual keyboard in Android

It explains how to change the action of the enter key.

Hope this helps!

Community
  • 1
  • 1
ThePerson
  • 3,048
  • 8
  • 43
  • 69
0

try

KeyboardView.OnKeyboardActionListener ... Listener for virtual keyboard events.

for further actions ...

http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener.html

KaySee
  • 423
  • 3
  • 7
  • 20