I am trying to change background of switch.It is changing but problem is some text is visible.
Below is my code
<Switch
android:id="@+id/switchsubsurf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:padding="5dip"
android:textOff=" "
android:textOn=" "
android:thumb="@drawable/toggle_button"
android:track="@drawable/toggle_bg" />
Inside the function use setBackgroundResource(R. color. button_color) function, this will set the background with color button_color.
Right-click on the drawable folder, go to new, and click on Drawable Resource File. Now set the name as custom_switch, root element as the selector, and click OK. Now add the below code to your file. The below code represents two states on the Switch, when true and when false.
make a drawer folder inside res
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_on" android:state_checked="true" android:state_focused="false"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_pressed="true"/>
<item android:drawable="@drawable/toggle_off" android:state_checked="false" android:state_focused="false"/>
</selector>
Then do this in xml
<Switch
android:id="@+id/saveLoginCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn=""
android:textOff=""
android:text=""
android:background="@drawable/toggle_selector"
android:thumb="@android:color/transparent"
android:track="@android:color/transparent"
/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With