0

I have this Switchbutton

<Switch
    android:id="@+id/switch"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:text="some text"
    android:textColor="@color/foreground" />

How can I change the color of the button when the button is checked from default to my color?

Update:

I've tried this solution: Change "on" color of a Switch

but when I change from Switch to SwitchCompat, I can't see any Switch Buttons more

FYI I use <style name="AppTheme" parent="Theme.Leanback">

dudi
  • 5,523
  • 4
  • 28
  • 57
  • Try https://stackoverflow.com/questions/11253512/change-on-color-of-a-switch. – ADM Jun 13 '18 at 07:57
  • Possible duplicate of [Change "on" color of a Switch](https://stackoverflow.com/questions/11253512/change-on-color-of-a-switch) – Sachin Rajput Jun 13 '18 at 08:00

1 Answers1

0

I have solved the problem by changing the color of the switch button programaticly

int myColor = getResources().getColor(R.color.myColor);
int defaultColor = getResources().getColor(R.color.defaultColor);

public void changeSwitch(View view) {
    boolean checked = ((Switch) view).isChecked();
    if (checked) {
        ((Switch) view).getThumbDrawable().setColorFilter(myColor,
                PorterDuff.Mode.MULTIPLY);
        ((Switch) view).getTrackDrawable().setColorFilter(myColor,
                PorterDuff.Mode.MULTIPLY);
    } else {
        ((Switch) view).getThumbDrawable().setColorFilter(defaultColor,
                PorterDuff.Mode.MULTIPLY);
        ((Switch) view).getTrackDrawable().setColorFilter(defaultColor,
                PorterDuff.Mode.MULTIPLY);
    }
}
dudi
  • 5,523
  • 4
  • 28
  • 57