Switch Track Color

Billabong picture Billabong · Oct 1, 2014 · Viewed 12.6k times · Source

I want to use a Switch and change its Track Color. So in my opinion nothing spectacular.

My Switch layout:

<Switch
    android:id="@+id/Switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:track="@color/gray"
    android:textOn="@string/on"
    android:textOff="@string/off"     
    android:text="@string/musiconoff" />

and my color "grey":

<color name="gray">#666666</color>

My Problem is that the Switch is shown as a 1-pixel Switch. It's just a small line. If I delete the "color"-line, the Switch is correct (without grey of course).

Where is my fault?

Answer

silentsudo picture silentsudo · Apr 20, 2016

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);
                }