user1406716 user1406716 - 1 month ago 5
Android Question

How to flip the checked status of a Switch only after confirm dialog?

My problem is - when I click on a

, it gets toggled first and then the
OnCheckedChangeListener
is called.

What I would like is this:

<Switch>
is clicked --> I show an AlertDialog --> If pressed yes or no --> Then flip ` with setChecked(boolean), [boolean = true if pressed yes, and false if pressed no].

Problem: When
<Switch>
is clicked, it gets flipped automatically. I want to qualify it with a yes or no from a AlertDialog first.

sw_enableDisable
.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());

alertDialogBuilder
.setMessage(
"Sure you want to enable?. ")
.setCancelable(true)
.setPositiveButton(
getString("YES"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {

dialog.cancel();
}
})

.setNegativeButton(
getString("NO"),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {

dialog.cancel();
}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
sw_enDis_alreadyClicked = true;

} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
alertDialogBuilder
.setMessage(
"Sure you want to disable?")
.setCancelable(true)
.setPositiveButton(
getString("YES"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
})

.setNegativeButton(
getString("NO"),
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {

dialog.cancel();
}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
});

Answer

Try this. Works for me

    Switch switchButton=(Switch)rootView.findViewById(R.id.switch_yours_id);


    switchButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            final Switch btn=(Switch)v;
            final boolean estado=btn.isChecked();

            if(btn.isChecked())btn.setChecked(false);
            else btn.setChecked(true);


            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        if(estado)btn.setChecked(true);
                        else btn.setChecked(false);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //No button clicked
                        break;
                    }
                }
            };

            String mensaje="Disable some function?";
            if(!btn.isChecked()) mensaje="Enable some function?";

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage(mensaje).setPositiveButton("YES", dialogClickListener)
                .setNegativeButton("NO", dialogClickListener).show();


        }
    });