0

Similar question again link. But the thread is old and can figure it out. I would like to change the state of switchA from within switchB and vice versa, but don't want to trigger the code in the listener. The code needs to be executed only when the switch is pressed. I got as far as:

switchA.setOnCheckedChangeListener (null);
                switchA.setChecked(false);

but this disables the switchA listener entirely. How to turn/activate it on agian?

Thanks.

new_project
  • 281
  • 1
  • 3
  • 10

1 Answers1

0

You can simply call switchA.setOnCheckedChangeListener(this); again:

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //This is the listener
        switchA.setOnCheckedChangeListener(this);

        //Disable the listener
        switchA.setOnCheckedChangeListener(null);
        //set state of Switch
        switchA.setChecked(false);
        //Enable the Listener again
        switchA.setOnCheckedChangeListener(this);

    }

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

    }
}
Binozo
  • 145
  • 2
  • 10