Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: Softkeyboard perform action when Done key is pressed

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.

like image 986
Khawar Raza Avatar asked Sep 28 '11 13:09

Khawar Raza


People also ask

How do I change the Done button on my Android keyboard?

If you want the kb to hide when clicking Done , and you set android:imeOptions="actionDone" & android:maxLines="1" without setting your EditText inputType it will NOT work as the default inputType for the EditText is not "text" as a lot of people think.

How to handle keyboard in Android studio?

To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.

What is Android soft keyboard?

The Android system shows an on-screen keyboard — known as a soft input method — when a text field in your UI receives focus. The keyboard takes about half the screen; in other words, you only have half of the screen to display any information.

How to hide soft keyboard in Android activity?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow , passing in the token of the window containing your focused view. This will force the keyboard to be hidden in all situations.


2 Answers

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;     } }); 
like image 161
Adil Soomro Avatar answered Oct 13 '22 06:10

Adil Soomro


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;                 }             }); 
like image 25
Silambarasan Poonguti Avatar answered Oct 13 '22 04:10

Silambarasan Poonguti