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 this is a simple piece of code, but where can I find an example of it?
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 this is a simple piece of code, but where can I find an example of it?
The short answer
In your
OnClick
listener call theonEditorAction
of theEditText
withIME_ACTION_DONE
The drill-down
I feel this method is better, simpler and more aligned with Android's design pattern. In the simple example above (and usually in most of the common cases) you'll have an
EditText
that has/had focus and it also usually was the one to invoke the keyboard in the first place (it is definitely able to invoke it in many common scenarios). In that same way, it should be the one to release the keyboard, usually that can be done by anImeAction
. Just see how anEditText
withandroid:imeOptions="actionDone"
behaves, you want to achieve the same behavior by the same means.Check this related answer
This worked for me for all the bizarre keyboard behavior
Add to your activity
android:windowSoftInputMode="stateHidden"
in Manifest file. Example:If all the other answers here don't work for you as you would like them to, there's another way of manually controlling the keyboard.
Create a function with that will manage some of the
EditText
's properties:Then, make sure that onFocus of the
EditText
you open/close the keyboard:Now, whenever you want to open the keyboard manually call:
And for closing call:
This should work:
I got one more solution to hide keyboard:
Here pass
HIDE_IMPLICIT_ONLY
at the position ofshowFlag
and0
at the position ofhiddenFlag
. It will forcefully close soft Keyboard.