Kubi
Kubi

Reputation: 73

close Alert Dialog without using negative Buttons

I created an AlertDialog, which will either refresh the activity (when btnConfirm1 is pressed), or does nothing and simply closes down (when btnDisconfirm1 is pressed). Everything is working apart from btnDisconfirm1. How can I close the dialog?

So apparently AlertDialog does not have a dismiss or cancel method, but is there another way without using negative buttons? The thing is, I created a layout file for this dialog and I don't know how to put a negative button in my xlm-file.

Or should I use a completely different approach apart from AlertDialog? Thanks!

    btnClear = (Button) findViewById(R.id.btnClear);
    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog.Builder mBuilder2 = new AlertDialog.Builder(ScoreScreen.this);

            View mView2 = getLayoutInflater().inflate(R.layout.dialog_confirm_delete, null);

            Button btnConfirm1=(Button) mView2.findViewById(R.id.btnConfirm1);
            Button btnDisconfirm1=(Button) mView2.findViewById(R.id.btnDisconfirm1);

            btnConfirm1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    player1.setPlayerScore(0);
                    player2.setPlayerScore(0);
                    player3.setPlayerScore(0);
                    player4.setPlayerScore(0);

                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            });

            btnDisconfirm1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //WHAT DO I PUT HERE???
                }
            });
            mBuilder2.setView(mView2);
            AlertDialog dialog = mBuilder2.create();
            dialog.show();
        }
    });

Upvotes: 0

Views: 543

Answers (2)

simpleApps
simpleApps

Reputation: 774

First you should create the AlertDialog with AlertDialog mDialog = mBuilder2.create();

And secondly you can dismiss the dialog inside the OnClickListener with mDialog.dismiss();

Upvotes: 1

Khemraj Sharma
Khemraj Sharma

Reputation: 59004

You can put this method in your Util class. and use callbacks

public interface OnDialogDismiss {
    void onPositiveClick();

    void onNegativeClick();
}

Modify it as your requirement.

public void showDialogForMultipleCallback(Context context, String title, String message, boolean cancellable, String neutralBbtn, String negativeBtn, String positiveBtn, final OnDialogDismiss onDialogDismiss) {
    final AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
    builder1.setTitle(title);
    builder1.setMessage(message);
    builder1.setCancelable(cancellable);
    if (neutralBbtn != null) {
        builder1.setNeutralButton(neutralBbtn, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (alert11 != null)
                    alert11.dismiss();
            }
        });
    }
    if (negativeBtn != null) {
        builder1.setNegativeButton(negativeBtn,
                new OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (alert11 != null)
                    alert11.dismiss();
                    }
                });
    }
    if (positiveBtn != null) {
        builder1.setPositiveButton(positiveBtn, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                onDialogDismiss.onPositiveClick();
            }
        });
    }
    alert11 = builder1.create();
    alert11.show();
}

Upvotes: 0

Related Questions