Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the track color of a SwitchCompat

I've tried using the following link to change the color of a SwitchCompat:

How to change the color of a SwitchCompat

Notice the low constrast in my switch:

The SwitchCompat

But after changing all relevant color values the track (the brighter grey) of the SwitchCompat remains the same. I don't want to change the appearance except the color. The thumb is in pink, and I want the track to have some contrast. Did I miss to define a value in my styles.xml?

I tried these values (random non-white colors):

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">     <item name="colorPrimary">@color/first</item>     <item name="colorPrimaryDark">@color/second</item>     <item name="colorAccent">@color/third</item>    ...     <item name="colorControlActivated">@color/first</item>     <item name="colorControlHighlight">@color/first</item>     <item name="colorControlNormal">@color/second</item>     <item name="colorSwitchThumbNormal">@color/second</item>     <item name="colorButtonNormal">@color/second</item> ...> 
like image 714
Close Call Avatar asked Jan 08 '15 17:01

Close Call


People also ask

How can I change my SwitchCompat color?

First, you should take a look to appCompat lib article there and to different attributs you can set: colorPrimary: The primary branding color for the app. By default, this is the color applied to the action bar background. colorPrimaryDark: Dark variant of the primary branding color.

What is SwitchCompat?

SwitchCompat is a complete backport of the core Switch widget that brings the visuals and functionality of the toggle widget to older versions of the Android platform. Unlike other widgets in this package, SwitchCompat is not automatically used in layouts that include the <Switch> element.


2 Answers

I had same probrem and solved it.

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">    ...    <!-- Active thumb color & Active track color(30% transparency) -->    <item name="colorControlActivated">@color/theme</item>    <!-- Inactive thumb color -->    <item name="colorSwitchThumbNormal">@color/grey300</item>    <!-- Inactive track color(30% transparency) -->    <item name="android:colorForeground">@color/grey600</item>    ... </style> 

I read app compat code, and understand it.

android.support.v7.internal.widget.TintManager.java

private ColorStateList getSwitchTrackColorStateList() {     if (mSwitchTrackStateList == null) {         final int[][] states = new int[3][];         final int[] colors = new int[3];         int i = 0;          // Disabled state         states[i] = new int[] { -android.R.attr.state_enabled };         colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.1f);         i++;          states[i] = new int[] { android.R.attr.state_checked };         colors[i] = getThemeAttrColor(R.attr.colorControlActivated, 0.3f);         i++;          // Default enabled state         states[i] = new int[0];         colors[i] = getThemeAttrColor(android.R.attr.colorForeground, 0.3f);         i++;          mSwitchTrackStateList = new ColorStateList(states, colors);     }     return mSwitchTrackStateList; } 
like image 133
Konifar Avatar answered Oct 03 '22 10:10

Konifar


Below is the AppCompat way of changing both the track and thumb color programmatically, for a specific SwitchCompat. For this example, I have hardcoded the thumbColor to red. Ideally, you would set the color through a second method parameter.

Please note that when the switch is checked, a ripple is displayed. The ripple color will not be changed by the code below.

public static void setSwitchColor(SwitchCompat v) {     // thumb color of your choice     int thumbColor = Color.RED;      // trackColor is the thumbColor with 30% transparency (77)     int trackColor = Color.argb(77, Color.red(thumbColor), Color.green(thumbColor), Color.blue(thumbColor));      // setting the thumb color     DrawableCompat.setTintList(v.getThumbDrawable(), new ColorStateList(             new int[][]{                     new int[]{android.R.attr.state_checked},                     new int[]{}             },             new int[]{                     thumbColor,                     Color.WHITE             }));      // setting the track color     DrawableCompat.setTintList(v.getTrackDrawable(), new ColorStateList(             new int[][]{                     new int[]{android.R.attr.state_checked},                     new int[]{}             },             new int[]{                     trackColor,                     Color.parseColor("#4D000000") // full black with 30% transparency (4D)             })); } 
like image 25
Ovidiu Avatar answered Oct 03 '22 12:10

Ovidiu