Use "ENTER" key on softkeyboard instead of clicking button

AndroidKeyboardAndroid Softkeyboard

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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 - Android

<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 - Android

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

android:imeOptions="actionSearch"

Solution 4 - Android

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 - Android

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
    }

Solution 6 - Android

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
}

Solution 7 - Android

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

Solution 8 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpeter.oView Question on Stackoverflow
Solution 1 - AndroidJulianView Answer on Stackoverflow
Solution 2 - AndroidAndrii KovalchukView Answer on Stackoverflow
Solution 3 - AndroiditemonView Answer on Stackoverflow
Solution 4 - Androidruyi.zhuView Answer on Stackoverflow
Solution 5 - AndroidArtem BotnevView Answer on Stackoverflow
Solution 6 - AndroidHassan TBTView Answer on Stackoverflow
Solution 7 - AndroidAlejandro SerretView Answer on Stackoverflow
Solution 8 - AndroidWojtek JaroszView Answer on Stackoverflow