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.
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
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
private void hideDefaultKeyboard() {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
//you have got lot of methods here
}
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.
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)
}
}
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
})