0

I'm trying to handle a keyboard response:

    EditText editText = (EditText) findViewById(R.id.password);
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            boolean handled = false;
            Log.d(TAG, "IME_ACTION_GO int:"+ IME_ACTION_GO+ " action id: "+ actionId);
           if (actionId == IME_ACTION_GO) {
                handled = true;
                Toast toast = Toast.makeText(getApplicationContext(),"GO pressed", Toast.LENGTH_SHORT);
                toast.show();
            }
            return handled;
        }
    });

and my layout xml is :

<EditText
    android:id="@+id/password"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:hint= "Password"
    android:inputType="textPassword"
    android:layout_alignBottom="@+id/button"
    android:layout_alignLeft="@+id/user_name"
    android:layout_alignStart="@+id/user_name"
    android:singleLine="true"
    android:imeOptions="actionGo"
    android:imeActionLabel="Login"/>

However, the actionID is always 0, whereas the int value for IME_ACTION_GO is 2. Therefore the if statement is never true.

user3559233
  • 105
  • 10
  • so is Logcat showing that they are both the same before your if statement? Log.d(TAG, "IME_ACTION_GO int:"+ IME_ACTION_GO+ " action id: "+ actionId); – Xjasz Mar 10 '15 at 16:56
  • They are not the same before the if statement. The values did not magically change, but from the beginning they were already 2 and 0 respectively. – user3559233 Mar 10 '15 at 16:59
  • so its null this might help you http://stackoverflow.com/questions/11301061/null-keyevent-and-actionid-0-in-oneditoraction-jelly-bean-nexus-7 – Xjasz Mar 10 '15 at 17:05

1 Answers1

0

I used Keyevent instead of onEditorAction to solve this:

    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int keyCode, KeyEvent event) {
            boolean handled = false;
           if (keyCode == KeyEvent.KEYCODE_ENTER) {
                handled = true;
                Toast toast = Toast.makeText(getApplicationContext(),"GO pressed", Toast.LENGTH_SHORT);
                toast.show();
            }
            return handled;
        }
    });
user3559233
  • 105
  • 10