101

Hello I've got a searched EditText and search Button. When I type the searched text, I'd like to use ENTER key on softkeyboard instead of search Button to activate search function.

Thanks for help in advance.

peter.o
  • 3,460
  • 7
  • 51
  • 77

8 Answers8

157

You do it by setting a OnKeyListener on your EditText.

Here is a sample from my own code. I have an EditText named addCourseText, which will call the function addCourseFromTextBox when either the enter key or the d-pad is clicked.

addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    addCourseFromTextBox();
                    return true;
                default:
                    break;
            }
        }
        return false;
    }
});
Julian
  • 20,008
  • 17
  • 77
  • 108
  • 4
    Actually, it is not guaranteed for soft keys. For example, it doesn't work for "ENTER" on Nexus 7 (Android 4.2) and for "BACK" it does. – Ghedeon Nov 28 '12 at 23:01
  • 4
    @Ghedeon you can set the `android:inputType="text"` xml for the edit text to show the enter key versus the default keyboard which has a carriage return. – Nick Jan 12 '13 at 06:08
  • 2
    This method isn't guarranteed to work as of Jellybean, see http://developer.android.com/reference/android/view/KeyEvent.html – Constantin Aug 06 '13 at 16:44
  • @Ghedeon so how do you get it to work for "ENTER" on Nexus 7? – HairOfTheDog Sep 05 '13 at 19:24
  • Looks like to handle software key, you have to use TextWatcher. OnKeyListener is for harwared keyboards, according to documantation. – Divers Dec 26 '13 at 08:35
  • 3
    This solution is completely broken on many devices, Nexus 7 included. Don't use it! – Stevey Apr 18 '17 at 06:14
  • Works for Soft Android Keyboard on the Nexus 6P. Thank you! – CrazedCoder Dec 03 '18 at 06:13
  • @user3562927 , using Nick's xml change, makes this snippet work just fine – PayToPwn Mar 06 '20 at 11:12
46
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

You can then listen for presses on the action button by defining a TextView.OnEditorActionListener for the EditText element. In your listener, respond to the appropriate IME action ID defined in the EditorInfo class, such as IME_ACTION_SEND. For example:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

Source: https://developer.android.com/training/keyboard-input/style.html

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Andrii Kovalchuk
  • 4,351
  • 2
  • 36
  • 31
28

may be you could add a attribute to your EditText like this:

android:imeOptions="actionSearch"
itemon
  • 590
  • 4
  • 8
5

add an attribute to the EditText like android:imeOptions="actionSearch"

this is the best way to do the function

and the imeOptions also have some other values like "go" 、"next"、"done" etc.

ruyi.zhu
  • 59
  • 1
2

Most updated way to achieve this is:

Add this to your EditText in XML:

android:imeOptions="actionSearch"

Then in your Activity/Fragment:

EditText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_SEARCH) {
        // Do what you want here
        return@setOnEditorActionListener true
    }
    return@setOnEditorActionListener false
}
Hassan TBT
  • 805
  • 7
  • 5
1

We can also use Kotlin lambda

editText.setOnKeyListener { _, keyCode, keyEvent ->
        if (keyEvent.action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
            Log.d("Android view component", "Enter button was pressed")
            return@setOnKeyListener true
        }
        return@setOnKeyListener false
    }
Artem Botnev
  • 2,267
  • 1
  • 14
  • 19
0

To avoid the focus advancing to the next editable field (if you have one) you might want to ignore the key-down events, but handle key-up events. I also prefer to filter first on the keyCode, assuming that it would be marginally more efficient. By the way, remember that returning true means that you have handled the event, so no other listener will. Anyway, here is my version.

ETFind.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (keyCode ==  KeyEvent.KEYCODE_DPAD_CENTER
        || keyCode ==  KeyEvent.KEYCODE_ENTER) {

            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // do nothing yet
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                        findForward();      
            } // is there any other option here?...

            // Regardless of what we did above,
            // we do not want to propagate the Enter key up
            // since it was our task to handle it.
            return true;

        } else {
            // it is not an Enter key - let others handle the event
            return false;
        }
    }

});
0

this is a sample of one of my app how i handle

 //searching for the Edit Text in the view    
    final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
        myEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (event.getAction() == KeyEvent.ACTION_DOWN)
                      if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                             (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                //do something
                                //true because you handle the event
                                return true;
                               }
                       return false;
                       }
        });
Alejandro Serret
  • 1,139
  • 15
  • 16