10

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?

gatzkerob
  • 877
  • 2
  • 12
  • 32

5 Answers5

10

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.

WilliamChik
  • 141
  • 2
  • 7
1
AlertDialog.Builder builder = new AlertDialog.Builder(EllipticalActivity.this);
builder.setTitle("title")
       .setMessage("message")
       .setCancelable(false)
       .setNegativeButton("Close", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               InputMethodManager inputManager = 
                   (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                                    InputMethodManager.HIDE_NOT_ALWAYS);
               dialog.cancel();
           }
        });
        AlertDialog alert = builder.create();
        alert.show();
Andrew Prock
  • 6,900
  • 6
  • 40
  • 60
Venkatesh S
  • 5,466
  • 1
  • 25
  • 30
  • Looking at your code, this will only close the keyboard if I click a button. I'd like it to close for more than one reason: clicking a button, clicking outside, or the dialog completing its task. It makes more sense to just detect when the dialog _is_ dismissed, rather than white-listing all the possible ways the window can _be_ dismissed. – gatzkerob Oct 08 '12 at 04:31
1

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
        }
    }
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

In my case, solution was to put keyboard hide in dialog dismiss

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}); 
Igori S
  • 123
  • 8
0

From Activity onCreateView() method you can do this:

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

Or In Manifest xml

android:windowSoftInputMode="stateAlwaysHidden"

It will automatically hide soft keyboard on Dismiss of Dialog

Amal Dev S I
  • 938
  • 13
  • 18