9

Right now I'm handling the enter key in my EditText fields using a an onEditorActionListener and looking at the Action ID for IME_NULL. It works fine for all my users except one. She has an Xperia Arc.

TextView.OnEditorActionListener keyListener = new TextView.OnEditorActionListener(){
  public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_NULL){
      if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etUser))){
        ((EditText) findViewById(R.id.etPass)).requestFocus();
      }
      if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etPass))){
        logon();
      }
    }
  return true;
  }
};

After learning about the issue, I tried another approach by using an onKeyListener and looking for the key event ACTION_DOWN then checking the keycode if it matched KEYCODE_ENTER.

EditText etUserName = (EditText) findViewById(R.id.etUser);
etUserName.setOnKeyListener(new OnKeyListener() {
  public boolean onKey(View view, int keyCode, KeyEvent event){
    if (event.getAction() == KeyEvent.ACTION_DOWN){
      switch (keyCode)
      {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
          if(((EditText)findViewById(view.getId())) == ((EditText)findViewById(R.id.etUser))){
            ((EditText) findViewById(R.id.etPass)).requestFocus();
          }
          return true;
        default:
          break;
      }
    }
    return false;
  }
});

No dice on that either. I'm at a loss right now. There are plenty of apps out there that handle the enter key just fine. what are they doing differently?

Mat
  • 202,337
  • 40
  • 393
  • 406
Scott
  • 493
  • 1
  • 5
  • 17
  • Have you by any chance found a workaround to this bug? I actually need to intercept the enter-press, not only keep the text on one row. – pgsandstrom Dec 06 '11 at 13:03
  • I solved it by handling all possible cases, and also allowing you to customize it a lot, if you really wish: https://stackoverflow.com/a/60989441/878126 – android developer Nov 26 '20 at 15:18

3 Answers3

17

I figured out how to get it to work.

I had to add android:singleLine="true" to the EditText tag in the layout XML (alternately you can set it by using setSingleLine() in code). This forces the edit text to use only one line and focus will go to the next EditText box.

Scott
  • 493
  • 1
  • 5
  • 17
  • Thanks for that solution. I tried to catch the enter key. Also, tried to jump to the next EditText when the enter key on virtual keyboard is hit. Until I saw this simple line that you provided, everything is solved. +1 for that! – detno29 Mar 30 '14 at 03:12
  • use `android:maxLines="1"` instead because `android:singleLine="true"` is deprecated – blueware Oct 09 '16 at 07:52
7

Try this solution: (I haven't tested it)

Set the following property to your EditText

android:imeOptions="actionNext"

Now you can set the following onEditorAction

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_NEXT) {
        // Program Logic Here
        return true;    
    }
    return false;
}

For some additional functionality, you can set your password EditText to:

android:imeOptions="actionDone"

So you can then just have something like this:

TextView.OnEditorActionListener keyListener = new TextView.OnEditorActionListener(){
        public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_NEXT) {
                ((EditText) findViewById(R.id.etPass)).requestFocus();
            }
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                logon();
            }
            return true;
        }
    };
jsw
  • 728
  • 1
  • 7
  • 16
  • Works fine on my device, nothing on the users. I gave her a test build with some debug code that displays the action id in a textview. nothing. I don't think it's even going into the onEditorAction method. – Scott Jun 19 '11 at 19:22
  • So there is not action id, no matter what she presses? Even if it is a character? – jsw Jun 20 '11 at 06:12
  • In onEditorAction, remember to return false if you do not consume the action. – Sofi Software LLC Apr 21 '15 at 22:44
0
(EditText) passwordView = (EditText) findViewById(R.id.password);
passwordView.setImeOptions(EditorInfo.IME_ACTION_DONE);
    passwordView.setOnEditorActionListener(new OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                String input;
                if(actionId == EditorInfo.IME_ACTION_DONE)
                {
                   input= v.getText().toString();
                    Toast toast= Toast.makeText(LogIn.this,input,
                            Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return true;
                }
                return false;
            }
        });