How to change height and width of Switch in Android

here is a small example how to change your swicth width, i hope it will help some one..

1)Use ur color for thumb (color_thumb.xml)

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


<size android:height="40dp"  />
<gradient android:height="40dp" android:startColor="#FF569BDA" android:endColor="#FF569BDA"/>

2)gray color for track (gray_track.xml)

shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"
>

<size android:height="40dp"  />
<gradient android:height="40dp" android:startColor="#dadadada" android:endColor="#dadadada"/>

3)Selector for thumb (thumb.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/gray_track" />
<item android:state_pressed="true"  android:drawable="@drawable/color_thumb" />
<item android:state_checked="true"  android:drawable="@drawable/color_thumb" />
<item                               android:drawable="@drawable/gray_track" />

4) Selector for track (track.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"  android:drawable="@drawable/color_thumb" />
<item                               android:drawable="@drawable/gray_track" />

and finally in switch

use

     android:switchMinWidth="56dp"
     android:thumb="@drawable/thumb"
    android:track="@drawable/track"

final int switchWidth = Math.max(
    mSwitchMinWidth,
    2 * mThumbWidth + paddingLeft + paddingRight);

final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;

The code above is method onMeasure() in class Switch, set android:switchMinWidth can not change the width properly, the only easy way is that change field mSwitchWidth by reflection dynamically:

try {
    Class clazz = Class.forName(Switch.class.getName());
    final Field switchWidth = clazz.getDeclaredField("mSwitchWidth");

    if (switchWidth != null) {
        switchWidth.setAccessible(true);        
        int width = widthWhateverYouWant;
        switchWidth.set(switchClassInstance, width);
        setMeasuredDimension(width, getMeasuredHeight());
        return;
    }
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (NoSuchFieldException e) {
    e.printStackTrace();
}

Do not forget setMeasuredDimension(width, getMeasuredHeight());, because measuredWidth of Switch is not base on field mSwitchWidth.

I guess it's not difficult to change height since you know how to change width :)


add this android:switchMinWidth="56dp" and try

<Switch
         android:id="@+id/plug_switch"
         android:layout_width="120dip"
         android:layout_height="10dp"
         android:maxHeight="10dp"
         android:thumbTextPadding="25dp"
         android:switchMinWidth="56dp"
         android:layout_below="@id/plug_graph_layout"
         android:layout_centerHorizontal="true"
         android:minWidth="100dp"