how to change state of a Switch programmatically andoir dstudio

how to change state of a Switch programmatically andlautturi dstudio

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
    }
}
Source‮tual.www:‬turi.com

In 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.

Created Time:2017-11-01 12:05:13  Author:lautturi