0

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Patrick Coyne
  • 61
  • 2
  • 6

1 Answers1

0

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));
        }
    }
});
Vahid H
  • 45
  • 6