Android custom switch gets cropped / cropped

I have a custom toggle switch for Android which is defined as

<Switch
    android:id="@+id/create_site_switch_ssl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|left"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:checked="true"
    android:gravity="center_vertical"
    android:thumb="@drawable/btn_gradient" />

      

and a thumb selector element like this

<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false">
    <shape android:shape="rectangle">    
        <gradient
            android:startColor="@color/Blue100"
            android:endColor="@color/Blue50"
            android:angle="90"/>    
        <padding android:left="@dimen/button_padding"
            android:top="@dimen/button_padding"
            android:right="@dimen/button_padding"
            android:bottom="@dimen/button_padding" />    
        <corners android:radius="@dimen/button_corner"/>    
        <stroke android:color="@color/BlueBorder" android:width="5dp"/>    
    </shape> 
</item>

      

In Android 4.4.2 version 4.3, the toggle slider is cut out as shown on the right and left sides.

enter image description here

I tried to play around with the shape and padding / margin / size swapping until nothing worked.

Also setting up a custom track with matching shims didn't work.

On Android 5.0 and later, the issue was resolved by installing

android:paddingLeft 
android:paddingRight

      

Why doesn't padding work for later versions? What am I missing? Any hints for a solution are appreciated!

+3


source to share


2 answers


I had the same problem. Removing the shim properties from the drawing fixed this for me. In your case, it will look like this:

<item android:state_enabled="true" android:state_focused="false" android:state_pressed="false">
    <shape android:shape="rectangle">    
        <gradient
            android:startColor="@color/Blue100"
            android:endColor="@color/Blue50"
            android:angle="90"/>    
        <corners android:radius="@dimen/button_corner"/>    
        <stroke android:color="@color/BlueBorder" android:width="5dp"/>    
    </shape> 
</item>

      



Then you can add an addition to the switch:

<Switch
    android:id="@+id/create_site_switch_ssl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|left"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:paddingLeft="50dp"
    android:paddingRight="50dp"
    android:checked="true"
    android:drawablePadding="@dimen/button_padding"
    android:gravity="center_vertical"
    android:thumb="@drawable/btn_gradient" />

      

+1


source


I know this is late, but this is for other people having the same problem. For me, the solution was to install negative padding. Therefore, if your switch is off on the right side, set the correct padding to a negative number, such as -5 or -10, depending on which part of the switch is off.



0


source







All Articles