Switch color

I want to use a switch and change its track color. So, in my opinion, nothing impressive.

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 is "gray":

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

My problem is that the switch appears as a 1-pixel switch. This is just a small line. If I remove the “color” line, the switch will be correct (without gray, of course).

Where is my fault?

+4
source share
4 answers

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);
                }
+9
source

You might want to create a drawing with your gray color, for example, and place it in the res / drawable folder:

track.xml:

<shape 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="rectangle">

     <size android:width="48dp" android:height="22dp" />
     <solid
         android:color="#666666" />
     <stroke
        android:width="2dp"
        android:color="#666666"
        android:dashWidth="2dp"
        android:dashGap="1dp" />
</shape>

, , ( ), .

?

+2

Try changing the color of the switch track

 android:track="@null"
 android:background="@drawable/img_setings_toggle_on"
0
source
android:track="@android:color/darker_gray"

Try this in your code

-2
source

All Articles