Android Quick Tip
Close the keyboard when you click ‘DONE’… seems like a simple task, right? Unfortunately, clicking ‘DONE’ or the ‘RETURN’ button in even a single line Android EditText will not drop focus and close the soft keyboard. In most cases it will just generate a new line of text. So what do you have to do if you want to close the soft keyboard by clicking ‘DONE’?
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.
* example_edittext.xml
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter some text"
android:imeOptions="actionDone"
/>
Now we need to create a custom OnEditorActionListener for the target EditText that will recognize when the ‘DONE’ button has been clicked. In it we override the onEditorAction() method, get an instance of the InputMethodManager, and use it to close the soft keyboard. Here’s the code for the custom OnEditorActionListener class:
* DoneOnEditorActionListener.java
class DoneOnEditorActionListener implements OnEditorActionListener { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); return true; } return false; } }
Now go to the onCreate() method of the Activity that contains the target EditText. We’ll call it SampleActivity here. In here you will assign the new DoneOnEditorActionListener to the target EditText via the setOnEditorActionListener() method.
* SampleActivity.java
public class SampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_activity_layout); // sample_activity_layout contains our target EditText, target_edittext EditText targetEditText = (EditText)findViewById(R.id.target_edittext); targetEditText.setOnEditorActionListener(new DoneOnEditorActionListener()); // The rest of the onCreate() code } }
And there you go, your target EditText field will now close the soft keyboard whenever you click the ‘DONE’ button. Very handy and not too difficult. Enjoy.
That huge confusing mess of code… just to make a common/normal “done” mean “done”?
Wow.
And if you have 5 editText boxes… it’s even a bigger mess.
Does that code only work if the keyboard button says DONE?
How would I do it if it was a SEARCH button?
very Nice Information
Thanks, It helped me.
Very useful! Thank you very much!
Maybe it is easier to put an line
android:singleLine=”true”
into the edittext tag in the layout xml file …
Thanks for sharing….it helped me.
Excellent, thanks !
android:imeOptions=”actionDone” doesn’t work but setting
sets the keyboard RETURN key to DONE but it will also limit the entry to one singe line of text.
Awesome…. works perfect! Thanks a lot
Thanks this was very helpfull!
I’ve added v.clearFocus(); to the class to not only hide the keyboard, but also clear the focus