52

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.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Mak
  • 1,017
  • 1
  • 11
  • 29

15 Answers15

95

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.

  • 6
    Why do you use same code line for showing and hiding? – János Jun 21 '16 at 11:43
  • 6
    According to http://stackoverflow.com/q/3568919/3990767 and http://stackoverflow.com/q/4745988/3990767, it is quite difficult to check if the keyboard is shown, so toggling might not be a good idea if you don't know if it is already shown. – SOFe Jul 31 '16 at 08:11
  • The problem with this approach is, if the user has hidden the keyboard, the second call will show up the keyboard again – Alexander Sep 26 '19 at 23:52
41
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditTextName.getWindowToken(), 0);
Jana
  • 2,850
  • 4
  • 33
  • 44
34

Use this working code :

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
andrewsi
  • 10,954
  • 132
  • 33
  • 48
Reddy Raaz
  • 698
  • 8
  • 9
  • it works, thanks. I want to hide the keyboard when user click Login button, in this case, this solution works better than `imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)`. because in landscape phone, the keyboard cover the Login button, and user have to hide the keyboard manually. – li2 Jun 12 '17 at 09:52
9

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);
        }
    }
}
gnat
  • 6,199
  • 101
  • 49
  • 71
Reddy Raaz
  • 698
  • 8
  • 9
3

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);
        }
Community
  • 1
  • 1
2

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.

uowaep
  • 41
  • 3
  • Well, `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 http://developer.android.com/reference/android/view/inputmethod/InputMethodManager.html#RESULT_UNCHANGED_SHOWN – ForceMagic Dec 17 '13 at 23:12
  • Sure, I understand, but shouldn't that go in the 2nd parameter? The first parameter is for showFlags. `public void toggleSoftInput (int showFlags, int hideFlags) ` I think this is more appropriate: `imm.toggleSoftInput(InputMethodManager.RESULT_SHOWN, InputMethodManager.RESULT_UNCHANGED_SHOWN);` – uowaep Dec 17 '13 at 23:50
  • Good point! :) Actually, there is no Enum Flag which supports the 0 value in the ShowFlags parameter. By reading the doc again I realized that they specify it can be 0. Sorry, my bad, although I don't find it a good practice not having provided a default Enum flag. – ForceMagic Dec 18 '13 at 00:01
  • 1
    I've been looking into it as well, seems like you're supposed to use magic numbers here. I added a link to the docs. – uowaep Dec 18 '13 at 00:07
2

This works fine

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

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);
    }
Boris Treukhov
  • 16,478
  • 9
  • 66
  • 86
0
InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Munawir
  • 3,196
  • 8
  • 31
  • 47
Priyanka
  • 291
  • 1
  • 6
  • 8
0

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

varotariya vajsi
  • 3,579
  • 32
  • 35
0

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);
}
tony gil
  • 9,063
  • 6
  • 72
  • 89
0

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

Gibolt
  • 24,018
  • 9
  • 129
  • 89
0
  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);
0

You could also try

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mseo
  • 3,711
  • 3
  • 19
  • 26
-3
private void close() {
    this.requestHideSelf(0);
}

this method is very simple

Vincent Williams
  • 1,908
  • 3
  • 12
  • 21