Android dismiss keyboard

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


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

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

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

you can also use this code on button click event


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

// from fragment

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(;  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

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

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(

    return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

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

Once these are in place, just call:


Need Your Help

Exclude methods from code coverage with Cobertura

java code-coverage cobertura

Is there a way to exclude code from inclusion into Cobertura coverage reports? We have some methods that should not be included in the coverage report and therefore not drive down the coverage numb...

Container view disappearing on completeTransition:

ios objective-c uiview uiviewcontroller uiviewanimationtransition

I am using custom view controller transitions, UIViewControllerAnimatedTransitioning, to present and dismiss a view controller.