Switch Track Color
Following code solves the problem:
In you Activity/Fragment (XML):
<Switch
android:id="@+id/sMuteNotifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="1dp"
android:checked="true"
android:gravity="center_vertical"
android:switchMinWidth="56dp"
android:thumb="@drawable/bg_thumb" // Create this drawable
android:track="@drawable/bg_switch_states"/> // and this one too
bg_thumb :(Use this or any image you want)
Create this in your
drawables
folder
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/white"/>
<size android:width="28dp" android:height="28dp"/>
</shape>
bg_switch_states:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/bg_on_switch"/>
<item android:state_checked="false" android:drawable="@drawable/bg_off_switch"/>
</selector>
bg_on_switch :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryLight"/> // <---What ever color you want to use here
</shape>
bg_off_switch :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryDark"/> // <---What ever color you want to use here
</shape>
There you go...
Hope this will help someone ð
Nothing worked for me except this
if (isChecked) {
mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_true_color), PorterDuff.Mode.SRC_IN);
} else {
mSwtPrivacyView.getTrackDrawable().setColorFilter(ContextCompat.getColor(this, R.color.switch_track_checked_false_color), PorterDuff.Mode.SRC_IN);
}