Close/hide the Android Soft Keyboard
I have an EditText
and a Button
in my layout. After writing in the edit field and clicking on the Button
, I want to hide the virtual keyboard. I assume that there's a simple, one- or two-liner to make this happen. Where can I find an example of it?
Android
- asked 9 years ago
- B Butts
2Answer
You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow
, passing in the token of the window containing your focused view.
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
- answered 8 years ago
- G John
Also useful for hiding the soft-keyboard is:
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
);
This can be used to suppress the soft-keyboard until the user actually touches the editText View.
- answered 8 years ago
- Gul Hafiz
Your Answer