Patrick Coyne
Patrick Coyne

Reputation: 61

Android changing the color of switch states

I looked and many of the answers were deprecated for the older version. I was wonder which attribute changes the color of a SwitchCompat widget. I am looking to change both the on and off state colors when on I would like white while off would be another color

Upvotes: 0

Views: 697

Answers (1)

Vahid H
Vahid H

Reputation: 45

You can use a program to do this. i don't think any attribute in switch xml could help you. but maybe this could help you:

switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (b==true){
            //when Switch is On
            finalSwitchCompat.setTrackTintList(ColorStateList.valueOf(Color.GRAY));
            finalSwitchCompat.setThumbTintList(ColorStateList.valueOf(Color.GRAY));
        }
        else {
            finalSwitchCompat.setTrackTintList(ColorStateList.valueOf(Color.WHITE));
            finalSwitchCompat.setThumbTintList(ColorStateList.valueOf(Color.WHITE));
        }
    }
});

Upvotes: 0

Related Questions