Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide soft input keyboard when dialog closes

I'm opening a Dialog from within an Activity. When the dialog opens, I call

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

The problem is, when I close the dialog either by hitting a cancel button or clicking outside the dialog, the keyboard switches to a text keyboard and doesn't go away util I click the hardware back button. How can I dismiss the keyboard when the dialog is dismissed, and focus is returned to the previous window?

like image 814
gatzkerob Avatar asked Oct 08 '12 04:10

gatzkerob


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically 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 prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.

How do I hide my keyboard when typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen. To bring the keyboard back into view, tap the typing area.

How do I hide soft keyboard when EditText is focused?

setShowSoftInputOnFocus(false); to disable the software keyboard showing when the EditText is touched. the hideKeyboard(this); call in OnCreate to forcible hide the software keyboard.


2 Answers

I guess that this method of the activity can be useful to you.

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus)
        {
            Toast.makeText(MainActivity.this, "has focus", Toast.LENGTH_LONG).show();
                        // write code to remove keyboard
        }
    }
like image 44
MKJParekh Avatar answered Sep 27 '22 16:09

MKJParekh


in AndroidManifest.xml, set this property in your Activity that show the Dialog

android:windowSoftInputMode="stateAlwaysHidden"

Note! not stateHiddent, is stateAlwaysHidden. It will automatically hide soft keyboard on Dismiss of Dialog.

Hope that save your life.

like image 114
WilliamChik Avatar answered Sep 27 '22 17:09

WilliamChik