Use "ENTER" key on softkeyboard instead of clicking button

136,222

Solution 1

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;
    }
});

Solution 2

<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

Solution 3

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

android:imeOptions="actionSearch"

Solution 4

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.

Solution 5

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
    }
Share:
136,222
peter.o
Author by

peter.o

Updated on February 26, 2020

Comments

  • peter.o
    peter.o about 4 years

    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.

  • Ghedeon
    Ghedeon over 11 years
    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.
  • Nick
    Nick over 11 years
    @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.
  • Constantin
    Constantin over 10 years
    This method isn't guarranteed to work as of Jellybean, see developer.android.com/reference/android/view/KeyEvent.html
  • HairOfTheDog
    HairOfTheDog over 10 years
    @Ghedeon so how do you get it to work for "ENTER" on Nexus 7?
  • Divers
    Divers over 10 years
    Looks like to handle software key, you have to use TextWatcher. OnKeyListener is for harwared keyboards, according to documantation.
  • stevyhacker
    stevyhacker almost 8 years
    This is simpler solution if you are building a search edit text.
  • Stevey
    Stevey about 7 years
    This solution is completely broken on many devices, Nexus 7 included. Don't use it!
  • CrazedCoder
    CrazedCoder over 5 years
    Works for Soft Android Keyboard on the Nexus 6P. Thank you!
  • PayToPwn
    PayToPwn about 4 years
    @user3562927 , using Nick's xml change, makes this snippet work just fine
  • Weiyi
    Weiyi almost 4 years
    need to set android:inputType="text" as well