2

The code below defines an AlertDialog with 2 buttons. Clicking on either button calls the correct onClick method. As it is, each method just has a Log statement, yet clicking on either one causes the dialog to be dismissed. Why?

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setPositiveButton("Dismiss",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("MyFragment", "Positive button");
            }
        }).setIcon(android.R.drawable.ic_dialog_info);

builder.setNeutralButton("Send Email",
        new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Log.d("MyFragment", "Neutral button");
            }
        }).setIcon(android.R.drawable.ic_dialog_info);
builder.setMessage("Some message");
builder.setCancelable(false);

Dialog dialog = builder.create();
dialog.setTitle("Details");
dialog.setCancelable(false);
dialog.show();
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • See the second answer here. http://stackoverflow.com/questions/4016313/how-to-keep-an-alertdialog-open-after-button-onclick-is-fired – shujj May 07 '15 at 15:06
  • you must note that dialog being dismissed is proper behaviour – shujj May 07 '15 at 15:09

2 Answers2

1

Try to add this .setCancelable(false).

EDIT: Well, that didn't work, apparently this did the trick. ;)

Community
  • 1
  • 1
Fábio Santos
  • 199
  • 1
  • 2
  • 16
1

The dialog is dismissed because this is its default behaviour. If you want it to remain you could override the onShowListener and set the appropriate button click listeners there.

final AlertDialog dialog = builder.create();
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface d) {
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(...);
        }
    });
dev.ice
  • 81
  • 2