use switch in android studio code example

Example 1: switch material android tutorial

// To check a switch
switchMaterial.isChecked = true

// To listen for a switch's checked/unchecked state changes
switchMaterial.setOnCheckedChangeListener { buttonView, isChecked
    // Responds to switch being checked/unchecked
}

Example 2: android switch on change

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // do something, the isChecked will be
        // true if the switch is in the On position
    }
});

Example 3: switch case in android

switch(position) {
    case 0:
        setContentView(R.layout.xml0);
        break;
    case 1:
        setContentView(R.layout.xml1);
        break;
    default:
        setContentView(R.layout.default);
        break;
}

Tags:

Java Example