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?
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.
To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.
It's fine if it worked for you, I solved it with android:focusable="true" android:focusableInTouchMode="true" in the parent RelativeLayout. This answer totally worked for me, removing focus of editText AND closing keyboard.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With