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:<
SwitchCompat is a Material Design widget. when my activity extends AppCompatActivity and android:theme="@style/mySwitch" is worked.
Here is Switch Layout
-Adding theme to your switch to change the color of track.Check the style given below:-.
**Switch Compact**
<android.support.v7.widget.SwitchCompat
android:id="@+id/goOnlineBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:theme="@style/Switch_style/>
**Switch_style**
<style name="Switch_style" parent="Theme.AppCompat.Light">
<!-- active thumb & track color (30% transparency) -->
<item name="colorControlNormal">@android:color/white</item>
<item name="colorControlActivated">@android:color/blue</item>
<item name="colorSwitchThumbNormal">@android:color/white</item>
<item name="trackTint">@color/white</item>
</style>
Where trackTint will change your track color
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;
}
if you want to costumize the color of track.you can use this solution.
track selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/checked_color" android:state_checked="true" />
<item android:color="@color/checked_color" android:state_selected="true" />
<item android:color="@color/unchecked_color" android:state_checked="false" />
<item android:color="@color/unchecked_color" android:state_selected="false" />
where checked_color and unchecked_color are color of your choices.
styles.xml
<style name="mySwitchStyle" parent="@style/Widget.AppCompat.CompoundButton.Switch">
<!-- do here for additional costumization on thumb, track background,text appearance -->
</style>
<style name="mySwitchTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="switchStyle">@style/mySwitchStyle</item>
<item name="colorControlActivated">@color/red</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="trackTint">@color/track_selector</item>
</style>
layout file
<android.support.v7.widget.SwitchCompat
android:theme="@style/mySwitchTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>