Custom switcher in android with text in track

I have a custom switch. I want the text (on / off) to be in the track, not in the thumb.

I was thinking of setting a selector with text in a drawable and setting it as a switch track. But I am unable to set the text on the form. Any ideas how I can do this?

I also want to add padding to the track, I don't want my thumb to touch the track. This is how I want it to look:

I want the switch to be something like

This is the shape of my track:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:useLevel="false">
<corners
    android:radius="20dp" />
<size
    android:width="48dp"
    android:height="21dp" />
<stroke
    android:color="#bfbfbf"
    android:width="6dp"/>

      

+3


source to share


1 answer


Disable screenshot

enter image description here

Include screenshot

enter image description here

Include image

enter image description here

Disable Image

enter image description here



Image Thumb

enter image description here

switch_on_off.xml

    <?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/on_switch"/>
    <item android:state_checked="false" android:drawable="@drawable/off_switch"/>
    <item android:drawable="@drawable/off_switch"/>
</selector>

      

xml code

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switch_compat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    app:track="@drawable/switch_on_off"
    android:thumb="@drawable/on_switch_active"/>

      

Try the code above and let me know if you find any error .....

+2


source







All Articles