hipek
hipek

Reputation: 201

AlertDialog before selecting checkBox in Android

I have dialog with checkBox. Before selecting this checkBox i would like to ask user, that he is sure to select these checkBox. (I mean, I try to select checkBox and before this AlertDialog appear and ask whether I am sure)

In code it looks like this:

CheckBox cb = (CheckBox) v.findViewById(R.id.favouriteChkBox);

            if (cb.isChecked()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        context);
                builder.setTitle("Do you want to add this to favourite?");

                builder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                itemChecked.set(position, true);
                            }
                        });
                builder.setNegativeButton("no",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                dialog.cancel();
                            }
                        });
                builder.create().show();

            }

But this does not work. The AlertDialog is shown and no metter if I clicked "yes" or "no", checkBox is selected.

Can you tell me why this does not work? Thanks!

Upvotes: 2

Views: 1502

Answers (2)

stinepike
stinepike

Reputation: 54682

    final CheckBox cb = (CheckBox) findViewById(R.id.favouriteChkBox);

    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (cb.isChecked()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        TestGitActivity.this);
                builder.setTitle("Do you want to add this to favourite?");

                builder.setPositiveButton("yes",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                cb.setChecked(true);
                            }
                        });
                builder.setNegativeButton("no",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                cb.setChecked(false);
                            }
                        });
                builder.create().show();

            }

        }
    });

Upvotes: 2

Siddharth Lele
Siddharth Lele

Reputation: 27748

I suspect this has to with the fact that if the User chooses NO, the state of the CheckBox is not being changed in the OnClickListener for the NegativeButton. Try this code:

CheckBox cb = (CheckBox) v.findViewById(R.id.favouriteChkBox);
if (cb.isChecked()) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Do you want to add this to favourite?");

    builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            itemChecked.set(position, true);
            // ADD THIS TO YOUR CODE
            cb.setChecked(true);
        }
    });

    builder.setNegativeButton("no", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // ADD THIS TO YOUR CODE
            .setChecked(false);
            dialog.dismiss(); // CHANGE THE .cancel() TO dismiss()
        }
    });

    builder.create().show();
}

Upvotes: 0

Related Questions