Reputation: 1297
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?
Upvotes: 26
Views: 39007
Reputation: 51
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;
});
Upvotes: 1
Reputation: 1110
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)
}
}
Upvotes: 0
Reputation: 119
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
})
Upvotes: 3
Reputation: 21
private void hideDefaultKeyboard() {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//you have got lot of methods here
}
Upvotes: 2
Reputation: 5574
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
Upvotes: 20
Reputation: 21076
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
Upvotes: 13
Reputation: 16466
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.
Upvotes: 1