Ishan Dragenox
Ishan Dragenox

Reputation: 135

ToggleButton with condition using AlertDialog

So I have a toggle button with states connect and disconnect when connect is clicked, it connects normally but when disconnected it should first show the user an alert dialog and then get disconnected. This is the code that I am using.

connectButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            if(isChecked){
                getConnected();
            }else {
                showDialog("Do you want to disconnect?");
            }
        }
    });

private void showDialog(String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle(message)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    connectButton.setChecked(false);
                    getDisconnected();
                }
            })
            .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    connectButton.setChecked(true);
                    dialogInterface.dismiss();
                }
            }).create().show();
}

The problem is that when the button is unchecked, it displays the unchecked state and then shows the alert dialog.

I want the condition to be such that the toggle button can change to unchecked state only if the positive condition is clicked, if negative condition is clicked it should do nothing, without the need for manually setting of the checked states.

Upvotes: 0

Views: 232

Answers (1)

Ravi Kumar
Ravi Kumar

Reputation: 4528

Move connectButton.setChecked(true); from netural button and call it before creating the dialog. See the code below:

 private void showDialog(String message) {
        connectButton.setChecked(true); //Placed here
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle(message)
                .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        connectButton.setChecked(false);
                        getDisconnected();
                    }
                })
                .setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //Removed from here                           
                        dialogInterface.dismiss();
                    }
                }).create().show();
    }

Upvotes: 0

Related Questions