vihkat
vihkat

Reputation: 1035

Android AlertDialog ReOpen or Block Close

I have an AlertDialog with two callback function. When the user is click to "Yes" or "No" than the callback is calling.

 public AlertDialog getMydialog(final Context context, final MyCallback onSuccess,final MyCallback onCancel) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage("MSG");
        builder.setCancelable(false);
        builder.setTitle("TITLE");

        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                onSuccess.callback();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                onCancel.callback();
            }
        });

        return builder.create();
    }

My Callbacks:

myDialog = new MyClass(this).MyDialog(this, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-YES");
        }
    }, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-NO");
        }
    });
myDialog.show();

When i Click the NO than the dialog is dismissed. But can't show again. For example:

myDialog = new MyClass(this).MyDialog(this, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-YES");
        }
    }, new MyCallback() {
        @Override
        public void callback() {
            Log.d("test","test Click-NO");
            myDialog.show() // isnt work
        }
    });
myDialog.show();

Anyone have idea to Disable .dismiss when I click to "NO" button? Or Reopen the dialog?

Thanks!

Upvotes: 0

Views: 992

Answers (2)

rafsanahmad007
rafsanahmad007

Reputation: 23881

To prevent the Dialog to get Dismiss you can use setOnShowListener on the AlertDialog and add your code..

final AlertDialog mAlertDialog = getMydialog();  //get Dialog
mAlertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button button = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //your code
               //you can call dissmiss later 
            }
        });

        Button button2 = mAlertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                //your code
               //you can call dissmiss later 
            }
        });
    }
});
mAlertDialog.show();

Upvotes: 1

Parth Bhatti
Parth Bhatti

Reputation: 541

((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE) .setEnabled(false);

Set this property to your alert dialog. This will disable negative button

Upvotes: 0

Related Questions