Search code examples
androidandroid-togglebutton

Change State of ToggleButton programmatically


I have placed a togglebutton with id toggleButton

and I am using the code below to check if it's on or off

public void toggleClick(View view){

boolean on = ((ToggleButton) view).isChecked();
if(on){

}else{

}

}

Everything works fine.. I am trying to change the state of the toggle button, I tried a lot of codes through internet but none changes the state of the toggle to off.

I tried this:

ToggleButton toggleButtons;
toggleButtons  = (ToggleButton) findViewById(R.id.toggleButton); //In onCreate
toggleButtons.setChecked(false);

any ideas please?


Solution

  • Use setChecked(boolean checked) by example:

    toggleButtons.setChecked(true)