Search code examples
androidcolorswidgetandroid-switch

Switch Track Color


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?


Solution

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