android: Softkeyboard perform action when Done key is pressed

AndroidKeyListenerAndroid Softkeyboard

Android Problem Overview


I have an EditText. I want that after typing some text, when user presses the Done key of the softkeyboard, it should directly perform some search operation which I have also implemented in a button click event.

Android Solutions


Solution 1 - Android

Try this

editText.setOnEditorActionListener(new OnEditorActionListener() {        
	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if(actionId==EditorInfo.IME_ACTION_DONE){
			//do something
		}
	return false;
	}
});

Solution 2 - Android

Try this

It works both for DONE and RETURN.

EditText editText= (EditText) findViewById(R.id.editText);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

				@Override
				public boolean onEditorAction(TextView v, int actionId,
						KeyEvent event) {
					if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
							|| actionId == EditorInfo.IME_ACTION_DONE) {
						// Do your action
                        return true;
					}
					return false;
				}
			});

Solution 3 - Android

You catch the KeyEvent and then check its keycode. FLAG_EDITOR_ACTION is used to identify enter keys that are coming from an IME whose enter key has been auto-labelled "next" or "done"

if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
    //your code here

Find the docs here.

Second Method

myEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    int result = actionId & EditorInfo.IME_MASK_ACTION;
    switch(result) {
    case EditorInfo.IME_ACTION_DONE:
        // done stuff
        break;
    case EditorInfo.IME_ACTION_NEXT:
        // next stuff
        break;
    }
 }
});

Solution 4 - Android

Try this

This will work in both condition whether your keyboard is showing enter sign or next arrow sign

YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener()
    {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
        {
            if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT)
            {
                //Perform Action here 
            }
            return false;
        }
    });

if u r facing red line then do this... import Keyevent and EditorInfo by pressing alt+enter then all the errors remove it will properly.......

Solution 5 - Android

In Kotlin use

viewBinding.editText.setOnEditorActionListener { view, actionId, event ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        //react to action
    }
    false
}

You can also return true inside 'if' braces to consume event - this way keyboard won't go down when you press done

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
QuestionKhawar RazaView Question on Stackoverflow
Solution 1 - AndroidAdil SoomroView Answer on Stackoverflow
Solution 2 - AndroidSilambarasan PoongutiView Answer on Stackoverflow
Solution 3 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 4 - AndroidSunilView Answer on Stackoverflow
Solution 5 - AndroidAriorickView Answer on Stackoverflow