Code example for Switch

Methods: setEnabled, isChecked, setChecked, setOnCheckedChangeListener

0
            case BluetoothAdapter.STATE_TURNING_ON:
                mSwitch.setEnabled(false);
                break; 
            case BluetoothAdapter.STATE_ON:
                setChecked(true); 
                mSwitch.setEnabled(true);
                break; 
            case BluetoothAdapter.STATE_TURNING_OFF:
                mSwitch.setEnabled(false);
                break; 
            case BluetoothAdapter.STATE_OFF:
                setChecked(false); 
                mSwitch.setEnabled(true);
                break; 
            default: 
                setChecked(false); 
                mSwitch.setEnabled(true);
        } 
    } 
 
    private void setChecked(boolean isChecked) {
        if (isChecked != mSwitch.isChecked()) {
            // set listener to null, so onCheckedChanged won't be called 
            // if the checked status on Switch isn't changed by user click 
            if (mValidListener) {
                mSwitch.setOnCheckedChangeListener(null);
            } 
            mSwitch.setChecked(isChecked);
            if (mValidListener) {
                mSwitch.setOnCheckedChangeListener(this);
            } 
        } 
    } 
}