Meet: this is my switch!

Meet: this is my switch!

Here is its XML code:

<Switch android:id="@+id/check" android:checked="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@+id/param" android:layout_toRightOf="@+id/param"/> 

I want to repaint it from delicate pink to green. It seems to me that it will be more beautiful. I rummaged through many resources on the Internet, but did not find anything useful.

Tell me how to do this?

1 answer 1

By itself, the Switch obeys only the theme Material Design, provided by the developer. Therefore, Google has made an element called SwitchCombat , which is more willing to comply with the requirements. Here is how it is created in the activity XML file:

 ... <android.support.v7.widget.SwitchCompat android:id="@+id/switch_compat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginLeft="50dp" android:checked="false" android:text="SwitchCompat" android:textOff="OFF" android:textOn="ON" app:showText="true"/> ... 

To set the style for SwitchCombat , you first need to create the styles.xml and put lines like this into it:

 <style name="MySwitch" parent="Theme.AppCompat.Light"> <item name="trackTint">@android:color/цвет</item> </style> 

And, secondly, in the SwitchCombat itself register a link to the style:

 <android.support.v7.widget.SwitchCompat ... android:theme="@style/MySwitch" /> 

You can also change the style for the active / inactive state, the color of the lane, etc. You can read more here .

PS At the request of the vehicle, I add a color change for the slider itself:

 <style name="MySwitch" parent="Theme.AppCompat.Light"> ... <!-- Неактивное состояние --> <item name="colorSwitchThumbNormal">@android:color/цвет</item> <!-- Активное состояние --> <item name="colorControlActivated">@android:color/цвет</item> ... </style> 
  • @ M.Muratov, added information (at the end of the answer). - Daniel Chizhevsky