Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide keyboard on dialog dismiss

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?

like image 440
Jacek Kwiecień Avatar asked Apr 19 '13 14:04

Jacek Kwiecień


People also ask

How do I hide keyboard?

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. This will force the keyboard to be hidden in all situations.

How do I make my keyboard invisible on Android?

You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I make my keyboard always visible?

You must have an EditText in your layout and that need to extent EditText base class. then Override onKeyPreIme() method, and return True. Now your keyboard will be always visible and can't be dismissed by Back key.


2 Answers

From fragments onCreateView() method you can do this:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

It will automatically hide soft keyboard on Dismiss of Dialog

like image 89
Amal Dev S I Avatar answered Nov 03 '22 08:11

Amal Dev S I


In my case I was using:

android:windowSoftInputMode="adjustPan|stateVisible"

I deleted stateVisible:

android:windowSoftInputMode="adjustPan"

And onDismiss(), no need to call hideSoftInput method.

like image 44
Arnaud Avatar answered Nov 03 '22 08:11

Arnaud