Many a times we just want that a event should trigger on click of a particular button(like Enter button, Search Button, Done button) from the default soft keyboard of android.
Implementing this is very easy.
All you need to do is search the id of the button on which you want to perform your particular action and simply follow the below steps :
- Get the instance of your EditText from your view.
- Add OnEditorActionListener on your EditText.
- Implement the logic in the onEditorAction method of the OnEditorActionListener by first comparing the id of your button.
And it is done, lets have a look at the code for the same , I am sharing the code for Done button of the Soft keyboard. You can change the same as per your need.
CODE :
1 2 3 4 5 6 7 8 9 10 |
MyEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { if (id == EditorInfo.IME_ACTION_DONE) { // implement the code for what you want to do when the done button is pressed. return true; } return false; } }); |
NOTE : For seeing the id of other buttons, you can open the EditorInfo class of the package android.view.inputmethod or you can have a look at : https://developer.android.com/reference/android/view/inputmethod/EditorInfo.html