Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 942
Billabong Avatar asked Oct 01 '14 10:10

Billabong


3 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);
                }
like image 144
silentsudo Avatar answered Nov 18 '22 00:11

silentsudo


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 😎

like image 28
Ali Goher Shabir Avatar answered Nov 18 '22 00:11

Ali Goher Shabir


Here is a very simple solution, that worked for me. Just setup the xml attributes thumbTint and trackTint with color selectors. Track is the background component, thumb is the round component.

<com.google.android.material.switchmaterial.SwitchMaterial
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:thumbTint="@color/switch_thumb"
        app:trackTint="@color/switch_track" />

or even better add these items to your app theme to apply to all switches:

<item name="thumbTint">@color/switch_thumb</item>
<item name="trackTint">@color/switch_track</item>

switch_thumb.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/[your desired color]" android:state_checked="true" />
     <item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>

switch_track.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:color="@color/[your desired color]" android:state_checked="true" />
     <item android:color="@color/[your desired color]" android:state_checked="false" />
</selector>
like image 6
danihoo94 Avatar answered Nov 18 '22 01:11

danihoo94