I am currently showing softkeyboard using the following code
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
And Here I d'not bind the softkeyboard with Edittext because of that I had used the above code.
Now I want to close the SoftKeyboard so i am currently using the below code but it is not working.
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
Can Anyone suggest me what to use for closing the softKeyboard ?
Based on Below Answer I want to let you clear that I am not using EditText, I use Layout on which I want to show Keyboard and Hide keyboard. I want to send keyboard key event to remote area bcoz of that I didnot used editText.
17 Answers 17
I have tested and this is working:
...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
By the way, the second parameter of your code is not right, please have a look at here.
3 Comments
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
1 Comment
Use this working code :
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
1 Comment
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0). because in landscape phone, the keyboard cover the Login button, and user have to hide the keyboard manually.If you want, you can use entire class and call KeyboardUtil.hideKeyBoard(context) method wherever:
public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
{
try
{
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
catch (Exception e)
{
// Ignore exceptions if any
Log.e("KeyBoardUtil", e.toString(), e);
}
}
}
2 Comments
Close/hide the Android Soft Keyboard
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
it's working for me i hope it's work for you..
Open the Android Soft Keyboard
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
Comments
user942821's answer for hiding it works:
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
But this also works for me to hide it:
imm.toggleSoftInput(0, 0);
You might also want to try:
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
When using '0' in the first parameter sometimes the keyboard toggles on in the wrong places under weird circumstances that I haven't been able to figure out how to duplicate yet. I'm still testing this last example, but will update when I find out more.
See toggleSoftInput documentation page for more information.
4 Comments
InputMethodManager.SHOW_FORCED is just an enum which represent an int value. So of course it works, but that would be the same as if you set it to InputMethodManager.RESULT_UNCHANGED_SHOWN which holds the value 0. Enum should be always used where it needs to be. See developer.android.com/reference/android/view/inputmethod/… public void toggleSoftInput (int showFlags, int hideFlags) I think this is more appropriate: imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, InputMethodManager.RESULT_UNCHANGED_SHOWN);This works fine
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);
2 Comments
You could also try
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Comments
Here is s solution, which checks if keyboard is visible
public static void hideKeyboard(Activity activity) {
if (isKeyboardVisible(activity)) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
public static boolean isKeyboardVisible(Activity activity) {
///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
Rect r = new Rect();
View contentView = activity.findViewById(android.R.id.content);
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
return
(keypadHeight > screenHeight * 0.15);
}
Comments
For hiding keyboard,
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
Here, "mView" can be any view which is visible on screen
Comments
This code hides the keyboard from inside onItemClick of an AutoCompleteTextView
public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
// whatever your code does
InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
}
Comments
Use a Kotlin Extension to hide keyboard
// Simple, reuseable call anywhere in code
myView.hideKeyboard()
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
How to show soft keyboard
fun View.showKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}
Related further topics:
Simplify adding Done/Next... action to edittext: read this post
Remove requirement for ever using getSystemService: Splitties Library
Comments
public void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
and Call where you want
hideKeyboard(MainActivity.this);
Comments
To hide the keyboard programmatically::
fun hideKeypad() {
val view = currentFocus
if (view != null) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
Comments
Kotlin way Hide and Show the Android Soft Keyboard:
Create a Extension file in kotlin and add below code for hide/show the Keyboard.
> fun EditText.showKeyboard(){
requestFocus()
val insetsController = ViewCompat.getWindowInsetsController(this)
insetsController?.show(WindowInsetsCompat.Type.ime())
}
fun Activity.hideKeyboard(){
val insetsController = ViewCompat.getWindowInsetsController(window.decorView)
insetsController?.hide(WindowInsetsCompat.Type.ime())
}
Comments
private void close() {
this.requestHideSelf(0);
}
this method is very simple
((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(view.getWindowToken(),0);