W.K.S
W.K.S

Reputation: 10095

Custom switch appearing stretched

I've developed a custom drawable for Switch's thumb and track but the problem is that it's appearing stretched:

enter image description here

Does anyone have any idea what's causing this? My XML's are below:

Layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_toLeftOf="@+id/switch1"
        android:padding="5dp" />

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:thumb="@drawable/switch_thumb_selector"
        android:track="@drawable/switch_track_selector" />

    <TextView
        android:id="@+id/switchErrorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_below="@id/switch1"
        android:layout_centerHorizontal="true"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@android:color/holo_red_light"
        android:visibility="gone" />

</RelativeLayout>

switch_thumb_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/switch_thumb_inactive" android:state_pressed="true"/>
    <item android:drawable="@drawable/switch_thumb_inactive" android:state_checked="true"/>
    <item android:drawable="@drawable/switch_thumb_inactive"/>

</selector>

switch_track_selector.xml

<item android:drawable="@drawable/switch_track_green" android:state_checked="true"/>
<item android:drawable="@drawable/switch_track_white" android:state_checked="false"/>
<item android:drawable="@drawable/switch_track_white"/>

Green Track Drawable:

enter image description here

Upvotes: 1

Views: 853

Answers (1)

romtsn
romtsn

Reputation: 12002

I think thats bad behavior has appeared because you didn't draw "content" area. Try this drawable, I've tested and it was working for me. switch_bg.9.png

Upvotes: 1

Related Questions