Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I handle ImeOptions' done button click?

I am having an EditText where I am setting the following property so that I can display the done button on the keyboard when user click on the EditText.

editText.setImeOptions(EditorInfo.IME_ACTION_DONE); 

When user clicks the done button on the screen keyboard (finished typing) I want to change a RadioButton state.

How can I track done button when it is hit from screen keyboard?

Screenshot showing the bottom right 'done' button on the software keyboard

like image 221
d-man Avatar asked Jan 05 '10 05:01

d-man


People also ask

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

First you need to set the android:imeOptions attribute equal to actionDone for your target EditText as seen below. That will change your 'RETURN' button in your EditText's soft keyboard to a 'DONE' button.

What is imeOptions android?

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: Kotlin Java.

What is Ime action?

As stated in Android Documentation, IME Action directs the keyboard what type of action should be displayed. This depends on you (the developer) to change the IME Action according to your needs. For example, a text field intended to be used as a search bar would contain the search IME Action and so on…


2 Answers

I ended up with a combination of Roberts and chirags answers:

((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(         new EditText.OnEditorActionListener() {     @Override     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {         // Identifier of the action. This will be either the identifier you supplied,         // or EditorInfo.IME_NULL if being called due to the enter key being pressed.         if (actionId == EditorInfo.IME_ACTION_SEARCH                 || actionId == EditorInfo.IME_ACTION_DONE                 || event.getAction() == KeyEvent.ACTION_DOWN                 && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {             onSearchAction(v);             return true;         }         // Return true if you have consumed the action, else false.         return false;     } }); 

Update: The above code would some times activate the callback twice. Instead I've opted for the following code, which I got from the Google chat clients:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {     // If triggered by an enter key, this is the event; otherwise, this is null.     if (event != null) {         // if shift key is down, then we want to insert the '\n' char in the TextView;         // otherwise, the default action is to send the message.         if (!event.isShiftPressed()) {             if (isPreparedForSending()) {                 confirmSendMessageIfNeeded();             }             return true;         }         return false;     }      if (isPreparedForSending()) {         confirmSendMessageIfNeeded();     }     return true; } 
like image 196
Thomas Ahle Avatar answered Oct 02 '22 13:10

Thomas Ahle


Try this, it should work for what you need:


editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {     @Override     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {     if (actionId == EditorInfo.IME_ACTION_DONE) {        //do here your stuff f        return true;     }     return false;     }  }); 
like image 21
chikka.anddev Avatar answered Oct 02 '22 12:10

chikka.anddev