Android - Capture "Done" and "Enter" key events on soft keyboard
Asked Answered
S

2

9

I have a Login page in my app where there are elements as listed:

  • username (EditText)
  • password (EditText)
  • Login (Button)

On pressing Login, it will land into the main screen. The intention is to perform that same action when the user hits Done at the completion of keying in the password on soft keyboard on Samsung Galaxy S3; and Enter key of soft keyboard on HTC One X.

So, here is how the EditText of Password field is:

<EditText
    android:id="@+id/password_txt"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:imeOptions="flagNoExtractUi"
    android:inputType="textPassword"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="8dp"
    android:singleLine="true" />

In the Activity, whatever I tried is here:

EditText mPassword = (EditText) findViewById(R.id.password_txt);
mPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if(event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE){
                        Log.e("MyApp", " ------> IN EDITOR ACTION DONE");
                    }
                    return false;
                }
            });

I did try with keeping the imeOptions for the Password field as actionDone along with the flagNoExtractUi but it didn't work out.

Slattern answered 31/5, 2013 at 7:19 Comment(0)
S
3

I found a solution for this in reply by Asha which seems to work fine for Samsung Galaxy S3, S3 Mini, S2, Google Nexus Tab and hopefully for all devices of Samsung. For HTC it worked on HTC Desire X so far I have checked. For HTC One X, this doesn't work. There is this actionid for which the value is 5 which captures the enter key action of the soft keypad.

Slattern answered 10/6, 2013 at 12:12 Comment(0)
M
2

HTC for some reason don't support imeOptions, in general not all keyboards support it. See more at here. My recommendation is to make your own UI buttons for next/done/etc. functionality.

Ment answered 31/5, 2013 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.