androidbuttonuiswitchischecked

Only do something when two Switch Buttons are independently checked / activated


I have two switch buttons that trigger some actions. If I activate A, action A happens, if I activate B, action B happens but if I activate both A and B nothing happens. Here is my code:

switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                toggled_if_debug_activated();
                checker1 = true;
            }
            else {
                normal_main_list();
            }
        }
    });

    switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                toggled_if_image_mode_activated();
                checker2 = true;
            }
            else {
                normal_main_list();
            }
        }
    });

I've tried so many things but nothing worked. How do I add an action C (ex. toggled_if_all_checked) that is triggered ONLY when both of the Switches are on? As far as I've researched .setOnCheckedChangeListener doesn't support anything like (switch0 && switch1).setOnCheckedChangeListener(...). So how do I do this?

Thank you in advance and may your roads lead you to warm sands!

================================ EDIT - SOLUTION ============================

Liar's solution bellow worked almost flawlessly but I had some bugs, respectively when activating both switches and trying to deactivate only one of them, the output was bugging a little. Better explaining:

You activate Switch A => Output A; You activate Switch B => Output B; You activate Switch A && B => Output C; You deactivate either A or B and the program was showing Outputs B or A (Basically upside down working)

This is the code that I came up with:

switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked && switch1.isChecked())
                {
                    toggle_C();
                }
                else
                if(isChecked && !switch1.isChecked())
                {
                    toggle_A();
                }
                else
                if (!isChecked && switch1.isChecked())
                {
                    toggle_B();
                }
                else {
                    toggle_DEFAULT();
                }
            }
        });
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

                if(isChecked && switch0.isChecked())
                {
                    toggle_C();
                }
                else
                if(isChecked && !switch0.isChecked())
                {
                    toggle_B();
                }
                else
                if (!isChecked && switch0.isChecked())
                {
                    toggle_A();
                }
                else {
                    toggle_DEFAULT();
                }
            }
        });

Maybe it's not the most professional solution but it works fine and it's bug free. Thank you all that helped me! May your roads lead you to warm sands


Solution

  • You can check another switch state like this:

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && switch0.isChecked()) {
                    // Do something
                }
                else {
                    normal_main_list();
                }
            }
        });
    
        switch0.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked && switch1.isChecked()) {
                    // Do something
                else {
                    normal_main_list();
                }
            }
        });