0

I have implemented a custom switch (like iOS) in android. It's working perfectly in xhdpi devices (Nexus 4, moto g3). But can't reduce the track width of switch in Nexus S. its too lengthy.

custom_switch_thumb

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true">
    <shape
        android:shape="rectangle"
        android:visible="true"
        android:dither="true"
        android:useLevel="false">
        <gradient
            android:startColor="#66AAFF00"
            android:endColor="#6600FF00"
            android:angle="270"/>
        <corners
            android:radius="20dp"/>
        <size
            android:width="30dp"
            android:height="30dp" />
        <stroke
            android:width="4dp"
            android:color="#0000ffff"/>
    </shape>
</item>
<item android:state_checked="false">
    <shape
        android:shape="rectangle"
        android:visible="true"
        android:dither="true"
        android:useLevel="false">
        <gradient
            android:startColor="#ff0000"
            android:endColor="#ff0000"
            android:angle="270"/>
        <corners
            android:radius="20dp"/>
        <size
            android:width="30dp"
            android:height="30dp" />
        <stroke
            android:width="4dp"
            android:color="#0000ffff"/>
    </shape>
</item>

custom_switch_track

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:visible="true"
android:dither="true"
android:useLevel="false">
<gradient
    android:startColor="#27170432"
    android:endColor="#27170432"
    android:angle="270"/>
<corners
    android:radius="20dp"/>
<size
    android:width="60dp"
    android:height="30dp" />
</shape>

Switch placed in layout

 <Switch
            android:id="@+id/notification_client_switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="8dp"
            android:gravity="center"
            android:showText="true"
            android:switchMinWidth="0dp"
            android:switchTextAppearance="@style/SwitchTextAppearance"
            android:textOff="@string/no_label"
            android:textOn="@string/yes_label"
            android:thumb="@drawable/custom_switch_selector"
            android:track="@drawable/custom_switch_track" />

I have changed track width and Switch "android:switchminwidth" with different values but no effect is found.

Tijo Joseph
  • 231
  • 2
  • 7

1 Answers1

0

Code for toogle Button

 <ToggleButton
            android:id="@+id/notification_client_switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:background="@drawable/toggleBackground"
            android:checked="true"
            android:textOff=" "
            android:textOn=" " />

xml for your drawable toggleBackground

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/on_image"
 android:state_checked="true" />
<item android:drawable="@drawable/off_image"   
android:state_checked="false" />

Ankur1994a
  • 2,112
  • 2
  • 13
  • 18