To change the state of a Switch programmatically in Android Studio, you can use the setChecked method of the Switch class.
Here's an example of how you might do this in an activity:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Switch switchButton = findViewById(R.id.switch_button);
switchButton.setChecked(true); // Set the switch to the "on" position
}
}Sourcetual.www:turi.comIn this example, the setChecked method is used to set the state of the Switch to the "on" position. You can pass false to the setChecked method to set the switch to the "off" position.