How to close Android Soft KeyBoard programmatically?

AndroidAndroid Softkeyboard

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);

Solution 3 - Android

Use this working code :

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Solution 4 - Android

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);
        }
    }
}

Solution 5 - Android

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);
        }

Solution 6 - Android

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.

Solution 7 - Android

This works fine

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);

Solution 8 - Android

You could also try

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Solution 9 - Android

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);
    }

Solution 10 - Android

InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Solution 11 - Android

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

Solution 12 - Android

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);
}
                

Solution 13 - Android

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)
}

Simplify adding Done/Next... action to edittext: read this post

Remove requirement for ever using getSystemService: Splitties Library

Solution 14 - Android

  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);

Solution 15 - Android

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)
    }
}

Solution 16 - Android

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())
}

Solution 17 - Android

private void close() {
    this.requestHideSelf(0);
}

this method is very simple

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMakView Question on Stackoverflow
Solution 1 - Androiduser942821View Answer on Stackoverflow
Solution 2 - AndroidJanaView Answer on Stackoverflow
Solution 3 - AndroidReddy RaazView Answer on Stackoverflow
Solution 4 - AndroidReddy RaazView Answer on Stackoverflow
Solution 5 - AndroidAshutosh SrivastavaView Answer on Stackoverflow
Solution 6 - AndroiduowaepView Answer on Stackoverflow
Solution 7 - AndroidDaniel CherubiniView Answer on Stackoverflow
Solution 8 - AndroidmseoView Answer on Stackoverflow
Solution 9 - AndroidBoris TreukhovView Answer on Stackoverflow
Solution 10 - AndroidPriyankaView Answer on Stackoverflow
Solution 11 - Androidvarotariya vajsiView Answer on Stackoverflow
Solution 12 - Androidtony gilView Answer on Stackoverflow
Solution 13 - AndroidGiboltView Answer on Stackoverflow
Solution 14 - AndroidAdil MushtaqView Answer on Stackoverflow
Solution 15 - AndroidRennyView Answer on Stackoverflow
Solution 16 - Androidvinay6krView Answer on Stackoverflow
Solution 17 - AndroidVincent WilliamsView Answer on Stackoverflow