Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onKeyListener on EditText only responds to "Enter" key when pressed twice

I have an EditText that I want to respond to a user pressing the "Enter" key. Here is the relevant code:

EditText edittext = (EditText) findViewById(R.id.user_query);
edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_DPAD_CENTER)) {
            sendMessage(v);
            return true;
        }
        return false;
        }
    });

When I run this on my tablet (Toshiba Thrive), it works perfectly. However, on my phone (HTC Desire HD), the OnKeyListener doesn't fire at all when the "Enter" key is pressed... but works when "Enter" is pressed a second time. Why is that, and how can I fix it?

like image 481
Kalina Avatar asked Aug 07 '12 22:08

Kalina


1 Answers

With autocorrect on the phone, pressing "Enter" once selects the suggested spelling of the word. Pressing "Enter" the second time sends the key press to the app. It's a feature, not a bug.

like image 67
Kalina Avatar answered Sep 27 '22 17:09

Kalina