I want to show the soft-keyboard for something EditText
that has focus and that has been obtained programmatically (without being pressed). And close it when some event happens like pressing a Button
on the screen.
I want to show the soft-keyboard for something EditText
that has focus and that has been obtained programmatically (without being pressed). And close it when some event happens like pressing a Button
on the screen.
To show the soft-keyboard forcibly, you can use:
However, if you want to remove the focus from
editText
it is necessary for another toView
get the focus . So if you don't have another one,View
you will have to create anotherView
empty one and give it the focus .To close the virtual keyboard, you can use:
Translated from original: Close / hide the Android on-screen keyboard
You can force to hide the soft keyboard using the class
InputMethodManager
, calling the methodhideSoftInputFromWindow
, sending the token of the window that contains the focused view.This will force the keyboard to hide in all situations. In some cases, it will have to be passed
InputMethodManager.HIDE_IMPLICIT_ONLY
as the second parameter to ensure that the keyboard is only hidden when the user does not explicitly force it to appear (by holding down menu).However since
Android 4.1+
, you have to addview.clearFocus()
for it to work properly:In Kotlin the following worked for me in case you need it, use the corresponding between the activity or the fragment depending on where you want to make the call
Usage example in a snippet: