12

I am using this Material Dialog library and when I click the positive button, the onPositive function is called and the dialog is closed. How can I prevent the dialog from closing/dismissing?

Thanks for answers.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Ololoking
  • 1,577
  • 4
  • 22
  • 37

3 Answers3

30

Add autoDismiss(false) and dismiss the dialog manually in callback method.

  new MaterialDialog.Builder(mainActivity)
            .title(R.string.title)
            .autoDismiss(false)
            .content(R.string.content)
            .positiveText(R.string.positive)
            .negativeText(R.string.negative)
            .positiveColor(setColor())
            .onPositive((dialog, which) => {
                // do something positive here
                dialog.dismiss();
            })
            .onNegative((dialog, which) => {
                // do something negative here
                dialog.dismiss();
            })
            .negativeColor(setColor())
            .typeface(titleAndActions, contentAndListItems)
            .build()
            .show();
Djordje Tankosic
  • 1,975
  • 2
  • 20
  • 21
  • @DjordjeTankosic Thanks for the help it worked. I was trying to Override the click before. – Dexto May 04 '15 at 11:36
  • As of the latest version prior to v2 RC, `.callback()` is deprecated. You should use: `onPositive()` which takes the dialog and a which (option) as arguments. – saiyancoder Dec 06 '18 at 15:52
1

.autoDismiss no longer exists. You should use .setCancelable(false) instead.

  @NonNull
  @Override
  public MaterialAlertDialogBuilder setCancelable(boolean cancelable) {
    return (MaterialAlertDialogBuilder) super.setCancelable(cancelable);
  }
Pedro Sequeira
  • 311
  • 3
  • 4
0

autoDismiss() is deprecated. Use setCancelable():

MaterialAlertDialogBuilder(this)
                .setTitle(R.string.app_name)
                .setMessage(R.string.message)
                .setCancelable(false)
                .setPositiveButton("ok") { _, _ -> finish() }.show()

It prevents the dialog to dismiss when:

  • back is pressed
  • click outside the dialog
TabascoLosco
  • 145
  • 7