Material-dialogs: Cannot auto hide keyboard when dismiss the dialog

Created on 7 Jan 2015  路  9Comments  路  Source: afollestad/material-dialogs

I use customDialog that has editText, positiveBtn and negativeBtn.
I can auto show the keyboard. However, when user dismiss the dialog the keyboard still shown.

this is my code

boolean wrapInScrollView = true;
MaterialDialog dialog;
dialog = new MaterialDialog.Builder(activity)
.title(textView.getText().toString())
.customView(R.layout.dialog_edittext, wrapInScrollView)
.positiveText("Ok")
.negativeText("Cancel")
.cancelable(true)
.positiveColor(Color.parseColor("#03a9f4"))
.callback(new MaterialDialog.ButtonCallback() {
@Override public void onPositive(final MaterialDialog dialog) {
super.onPositive(dialog);
EditText dialogEditText = (EditText) dialog.getCustomView().findViewById(R.id.dialogEditText);
textView.setText(dialogEditText.getText().toString());
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

                     @Override public void onNegative(MaterialDialog dialog) {
                         super.onNegative(dialog);
                         dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                     }
                 })
                 .build();

//this statement will show the keyboard when show the dialog
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

/*EditText myEditText = (EditText) dialog.getCustomView().findViewById(R.id.dialogEditText);
InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                                                                               Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);*/
dialog.show();

Most helpful comment

After lot of research I found this solution

.dismissListener(new DialogInterface.OnDismissListener() {
  @Override
  public void onDismiss(DialogInterface dialog) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
   })

All 9 comments

This issue has nothing to do with the library.

You should be using the show and dismiss listeners for this rather than butto callbacks.

I try to use show&dismiss but it still doesn't show/hide keyboard.
MaterialDialog dialog;
dialog = new MaterialDialog.Builder(activity)
.title(textView.getText().toString())
.customView(R.layout.dialog_edittext, wrapInScrollView)
.positiveText("Ok")
.negativeText("Cancel")
.cancelable(true)
.positiveColor(Color.parseColor("#03a9f4")).showListener(new DialogInterface.OnShowListener() {
@Override public void onShow(DialogInterface dialog) {
MaterialDialog materialDialog = (MaterialDialog) dialog;
materialDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}).dismissListener(new DialogInterface.OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
MaterialDialog materialDialog = (MaterialDialog) dialog;
materialDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

            }
        })
                 .callback(new MaterialDialog.ButtonCallback() {
                     @Override public void onPositive(final MaterialDialog dialog) {
                         super.onPositive(dialog);
                         EditText dialogEditText = (EditText) dialog.getCustomView().findViewById(R.id.dialogEditText);
                         textView.setText(dialogEditText.getText().toString());

                     }

                     @Override public void onNegative(MaterialDialog dialog) {
                         super.onNegative(dialog);

                     }
                 })
                 .build()

I have the same problem, I don't arrive to close keyboard after a prompt dialog. Do you have a solution ?

After lot of research I found this solution

.dismissListener(new DialogInterface.OnDismissListener() {
  @Override
  public void onDismiss(DialogInterface dialog) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
   })

The upcoming release will have a built in dialog among lots of other things: https://github.com/afollestad/material-dialogs/commit/1ccca94bd0cac6601901f88d2a3d0c8b55f91cfe

After lot of research I found this solution

.dismissListener(new DialogInterface.OnDismissListener() {
  @Override
  public void onDismiss(DialogInterface dialog) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
   })

Worked thanks!

^ this exact thing is already done internally

After lot of research I found this solution

.dismissListener(new DialogInterface.OnDismissListener() {
  @Override
  public void onDismiss(DialogInterface dialog) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
   })

thanks

After lot of research I found this solution

.dismissListener(new DialogInterface.OnDismissListener() {
  @Override
  public void onDismiss(DialogInterface dialog) {
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    }
   })

it also works on BottomSheetDialog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bernaferrari picture bernaferrari  路  5Comments

eygraber picture eygraber  路  3Comments

azizimusa picture azizimusa  路  4Comments

ghost1372 picture ghost1372  路  5Comments

jahirfiquitiva picture jahirfiquitiva  路  6Comments