How to hide keyboard on dialog dismiss

Jacek Kwiecień picture Jacek Kwiecień · Apr 19, 2013 · Viewed 10.3k times · Source

I have an Activity with single Fragment on it. There is one EditText on the fragment.

The keyboard is popping up as soon the fragment is shown, however I managed to block it setting in the manifest android:windowSoftInputMode="stateHidden"

However, there also is a button, which opens a dialog with another EditText.

I have a method that automatically closes the keyboard on dialog dismiss.

public static void closeInput(final View caller) {      
    caller.post(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    });
}

The method is not a pretty hack and there is something wrong about it. Dialog's EditText has inputType="numberDecimal". The closeInput() seems to be not closing the keyboard, only changing it to the default alphabetical state.

What is going on here?

Answer

Jacek Kwiecień picture Jacek Kwiecień · Jun 30, 2013

In my case I used method:

public static void closeInput(final View caller) {  
    caller.postDelayed(new Runnable() {
        @Override
        public void run() {
            InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }, 100);
}

It was refusing to work properly because of activity settings in Manifest, if I recall you can't have android:windowSoftInputMode="any_of_these" set