0

I used below code for my edit text so that when I press enter it does not go to new line and works like pressing button but it does not work, what's the problem?

editText.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView arg0, int keyCode,
                KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_ENTER:
                    search();
                    return true;
                default:
                    break;
                }
            }
            return false;
        }
    });
InnocentKiller
  • 5,234
  • 7
  • 36
  • 84

2 Answers2

1

Add this in your XML EditText

  android:maxLines="1"
  android:singleLine="true"

It will automatically perform the same rather than going to new line. Enter will act like a button

MDMalik
  • 3,951
  • 2
  • 25
  • 39
1

try this, it may help

editText.setOnEditorActionListener(new EditText.OnEditorActionListener()
    {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId == EditorInfo.IME_ACTION_DONE)
            {
                search();
                return true;
            }
            return false;
        }

    });
Anil kumar
  • 1,534
  • 3
  • 14
  • 20