7

I meet this case, I check the soft keyboard is on and I want to dismiss in the code, when I use the below code it cannot dismiss the keyboard, because the code can not find any focus, but the keyboard is still on, so how can I hide it?

private void hideSoftKeyboard() {
    Activity activity = (Activity) sContext;
    View view = activity.getCurrentFocus();
    if (view != null) {

        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        //((Activity) sContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    } else {

            Log.i(sClassTag,"focus not found");

    }
}
newszer
  • 390
  • 2
  • 16
  • `View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); }` – Charuක Dec 20 '16 at 16:26
  • 1
    `InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);` – Charuක Dec 20 '16 at 16:27
  • Possible duplicate of [Close/hide the Android Soft Keyboard](http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – Charuක Dec 20 '16 at 16:28
  • Great! It works for me, thank you ! maybe you can move this to an answer, and I can vote. – newszer Dec 21 '16 at 01:57
  • Okay..glad to help :)) – Charuක Dec 21 '16 at 02:06
  • Use this for Hide keybord [enter link description here](https://stackoverflow.com/a/67088797/14653921) – Adil Mushtaq Apr 14 '21 at 09:13

3 Answers3

9

Try to use this

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.

View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

This will force the keyboard to be hidden in all situations. In some cases you will want to pass in InputMethodManager.HIDE_IMPLICIT_ONLY as the second parameter to ensure you only hide the keyboard when the user didn't explicitly force it to appear (by holding down menu).

or this

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

you can find more details here

Community
  • 1
  • 1
Charuක
  • 12,229
  • 5
  • 43
  • 83
0
InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
0

You can use these extension to toggle the keyboard:

fun Context.showKeyboard() {
    val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

fun Context.hideKeyboard() {
    val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

fun Context.toggleKeyboard() {
    val imm = this.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    if (imm.isActive) {
        hideKeyboard()
    } else {
        showKeyboard()
    }
}
Fori
  • 436
  • 5
  • 13
Mickael Belhassen
  • 1,755
  • 10
  • 28