Best code examples for Android Switch class (android.widget.Switch)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android Switch class.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

Android Switch examples from Open Source projects
This code example shows how to use the following methods: isChecked, setOnCheckedChangeListener, setChecked, setEnabled
            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);
            } 

8
CodeRank
This code example shows how to use the following methods: isChecked, setOnCheckedChangeListener, setChecked
                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);
            } 
        } 
    } 
} 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: setChecked, setEnabled
    } 
 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean desiredState) {
        boolean success = false;
        mActionBarSwitch.setEnabled(false);
        if (desiredState) {
            success = mNfcAdapter.enableNdefPush();
        } else { 
            success = mNfcAdapter.disableNdefPush();
        } 
        if (success) {
            mActionBarSwitch.setChecked(desiredState);
        } 
        mActionBarSwitch.setEnabled(true);
    } 
} 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: isChecked, setOnCheckedChangeListener, setChecked
    } 
 
    public void resume() { 
        mPrefs.registerOnSharedPreferenceChangeListener(this);
        setSwitchChecked(mPrefs.getBoolean(mKey, mDefValue));
        mSwitch.setOnCheckedChangeListener(this);
    } 
 
    @Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(mKey)) {
            setSwitchChecked(sharedPreferences.getBoolean(mKey, mDefValue));
        } 
    } 
 
    private void setSwitchChecked(boolean checked) {
        if (checked != mSwitch.isChecked()) {
            mStateMachineEvent = true;
            mSwitch.setChecked(checked);
            mStateMachineEvent = false;

8
CodeRank
This code example shows how to use the following methods: setOnCheckedChangeListener
        mSwitch.setOnCheckedChangeListener(this);
        setSwitchState(); 
    } 
 
    public void pause() { 
        mSwitch.setOnCheckedChangeListener(null);
    } 
 
    public void setSwitch(Switch switch_) {
        if (mSwitch == switch_) return;
        mSwitch.setOnCheckedChangeListener(null);
        mSwitch = switch_;
        mSwitch.setOnCheckedChangeListener(this);
        setSwitchState(); 
    } 
 
    private void setSwitchState() { 
        boolean enabled = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.SYSTEM_PROFILES_ENABLED, 1) == 1; 
        mStateMachineEvent = true;
Full Snippet Info
6
CodeRank
Related Android Switch Questions & Answers:
See Code Examples for Android Switch Methods: