I have a EditText control.
If I tap it the softkeyboard will popup however when I press "enter/ok/return" then the EditText control it still has focus and the keyboard up.
How do I close the softkeyboard and remove focus from it?

- 113,822
- 119
- 272
- 400

- 1,287
- 3
- 16
- 27
7 Answers
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

- 5,514
- 5
- 35
- 41
-
5This will only hide the keyboard. It will not remove focus. – lostintranslation Jan 31 '17 at 17:20
In the layout XML file, specify an imeOption on your EditText:
android:imeOptions="actionGo"
Next, add an action listener to your EditText in the Activity's java file
mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
return true;
}
return false;
}
});
Where mYourEditText is an EditText object

- 20,756
- 32
- 99
- 177
-
Hmm thank you. but im a suposed to write the imeOpt in the layout or the edittext? and i got error on EditorInfo, any idea? – carefacerz Nov 22 '10 at 16:11
-
-
1This will dismiss the keyboard but will not remove focus in all cases. IE android will request focus on the first available view that can focus. – lostintranslation Jan 31 '17 at 17:20
Make sure your EditText XML has :
android:id="@+id/myEditText"
android:imeOptions="actionDone"
Then set listener to your EditText (with Kotlin, and from a fragment):
myEditText.setOnEditorActionListener({ v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
myEditText.clearFocus()
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
false
})

- 119
- 8
You could try doing SetFocus()
on another element in your layout.
If you are talking about the "enter/ok/return" button on the keyboard itself you may have to set up a KeyListener
on the EditText
control in order to know when to SetFocus()
on another element.

- 15,691
- 12
- 62
- 103
-
2I understand that this is the solution given by many people, but I don't like it, because instead of input.clearFocus() simply working (i.e. clearing the focus from that input), you now have to set the focus to something else!? It just seems counter-intuitive. – marienke Apr 12 '17 at 13:45
You must catch the action en OnEditorActionListener
If you are in an Activity use:
editText.setOnEditorActionListener((viewAux, actionId, eventKey) -> {
if (actionId == EditorInfo.IME_ACTION_DONE){
//Remove focus
editText.clearFocus();
//Hide the keyboard
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
return false;
});
If you are in a Fragment add requireContext
editText.setOnEditorActionListener((viewAux, actionId, eventKey) -> {
if (actionId == EditorInfo.IME_ACTION_DONE){
editText.clearFocus();
InputMethodManager imm = (InputMethodManager)requireContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return true;
}
return false;
});

- 11
- 1
A Kotlin solution that works for me, removes all active focus and close the soft keyboard. Set android:focusableInTouchMode="true" on your parent layout. In my case I have a ScrollView as well. I am setting view.clearFocus() in its touch event listener. This will clear all the focus on any touched textview. Then I proceed to close the soft keyboard on screen.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
...
android:focusableInTouchMode="true" //<--- this is important
tools:context=".SomFragment">
<ScrollView
android:id="@+id/scroll_view_container"
...
>
<TextView
android:id="@+id/sone_text_1"
...
/>
<TextView
android:id="@+id/sone_text_2"
...
/>
Then in your class
scroll_view_container.setOnTouchListener { v, event ->
view.clearFocus()
hideSoftKeyboard()
true
}
private fun hideSoftKeyboard() {
val windowToken = view?.rootView?.windowToken
windowToken?.let{
val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(it, 0)
}
}

- 1,090
- 13
- 19