157

How do I dismiss the keyboard when a button is pressed?

1
  • Make a EditText Focusable = False does the job. Do you want to disable it completely?
    – st0le
    Aug 24, 2010 at 5:49

11 Answers 11

340

You want to disable or dismiss a virtual Keyboard?

If you want to just dismiss it you can use the following lines of code in your button's on click Event

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
4
  • 1
    Two problems with this.... one is that myEditText needs to be Final. Second is that I have to know which EditText box has the focus. Any solution to this? Jul 16, 2012 at 4:00
  • 83
    For anyone else who stumbles here, you can use the activity's (either the activity you are in or the fragments getActivity()) getCurrentFocus().getWindowToken() for the first arg to hideSoftInputFromWindow(). Also, do it in the onPause() and not the onStop() if you are trying to get it to go away when changing activities. Mar 21, 2013 at 19:17
  • 2
    This answer, combined with the comment up here, totally solved my problem!
    – aveschini
    Oct 3, 2013 at 8:26
  • 10
    What an ugly, ugly approach to dismiss a keyboard. I hope there's a cleaner way in the future to do such a simple thing.
    – Subby
    May 20, 2015 at 11:01
80

The solution above doesn't work for all device and moreover it's using EditText as a parameter. This is my solution, just call this simple method:

private void hideSoftKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

    if(imm.isAcceptingText()) { // verify if the soft keyboard is open                      
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}
2
  • 3
    isAcceptingText() made this answer better than others Apr 25, 2017 at 12:19
  • Good solution... in rare cases isAcceptingText() can return false when the keyboard is still on the screen, for example click away from EditText to text that can be selected but not edited in the same window.
    – Georgie
    Jan 22, 2019 at 2:17
30

This is my solution

public static void hideKeyboard(Activity activity) {
    View v = activity.getWindow().getCurrentFocus();
    if (v != null) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
1
  • That is if you know the view that has caused the keyboard to show.
    – RPM
    Dec 11, 2019 at 0:43
16

you can also use this code on button click event

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
0
15

Here's a Kotlin solution (mixing the various answers in thread)

Create an extension function (perhaps in a common ViewHelpers class)

fun Activity.dismissKeyboard() {
    val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
    if( inputMethodManager.isAcceptingText )
        inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}

Then simply consume using:

// from activity
this.dismissKeyboard()

// from fragment
activity.dismissKeyboard()
5

The first solution with InputMethodManager worked like a champ for me, the getWindow().setSoftInputMode method did not on android 4.0.3 HTC Amaze.

@Ethan Allen, I did not need to make the edit text final. Maybe you are using an EditText inner class that you declared the containing method? You could make the EditText a class variable of the Activity. Or just declare a new EditText inside the inner class / method and use findViewById() again. Also, I didn't find that I needed to know which EditText in the form had focus. I could just pick one arbitrarily and use it. Like so:

    EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
1
4
    public static void hideSoftInput(Activity activity) {
    try {
        if (activity == null || activity.isFinishing()) return;
        Window window = activity.getWindow();
        if (window == null) return;
        View view = window.getCurrentFocus();
        //give decorView a chance
        if (view == null) view = window.getDecorView();
        if (view == null) return;

        InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null || !imm.isActive()) return;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
1

This Solution make sure that it hides keyboard also do nothing if it not opened. It uses extension so it can be used from any Context Owner class.


fun Context.dismissKeyboard() {
    val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
    val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
    val height = windowHeightMethod.invoke(imm) as Int
    if (height > 0) {
        imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
    }
}


0

By using the context of the view, we can achieve the desired outcome with the following extension methods in Kotlin:

/**
 * Get the [InputMethodManager] using some [Context].
 */
fun Context.getInputMethodManager(): InputMethodManager {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return getSystemService(InputMethodManager::class.java)
    }

    return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}

/**
 * Dismiss soft input (keyboard) from the window using a [View] context.
 */
fun View.dismissKeyboard() = context
        .getInputMethodManager()
        .hideSoftInputFromWindow(
                windowToken
                , 0
        )

Once these are in place, just call:

editTextFoo.dismiss()
0

Kotlin:

To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.

Example:

mSearchView.clearFocus()
0

some edits in above solutions and worked

private void hideSoftKeyBoard() {
    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);

    if (imm.isAcceptingText()) { // verify if the soft keyboard is open
        imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.