11
  1. I woudld like to fit the switch in 120dp width and 30dp height.
  2. Also I want to no text in the thumb but would like to have the thumb cover half of the total width so i want it to be 60dp.
  3. Also, I want the height of the thumb to be little less than the height of the switch of the background could be seen from 3 sides.

I have no idea how to do #3 and for #1 and #2, I tried the following xml, but it is not working:

 <Switch
         android:id="@+id/plug_switch"
         android:layout_width="120dip"
         android:layout_height="10dp"
         android:maxHeight="10dp"
         android:layout_below="@id/plug_graph_layout"
         android:layout_centerHorizontal="true"
         android:minWidth="100dp"
         android:thumb="@drawable/plugs_button"
         android:track="@drawable/btntoggle_selector"
         android:textOn=""
        android:textOff=""
         android:width="120dip" />

could someone please help me with #1, #2 and #3.

Thanks

Sunny
  • 7,444
  • 22
  • 63
  • 104

3 Answers3

22

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"
Nambi
  • 11,944
  • 3
  • 37
  • 49
  • 1
    But this will just make sure that the minWidth does not go below 56dp. I want it to be at 120dp. so how does this help? – Sunny Dec 24 '13 at 14:42
  • 1
    i did, but the point is that my image is larger than 120 dp and hieght is greater than 10 dp. but i want to fix it at 120 and 10 respectively – Sunny Dec 24 '13 at 14:47
  • 1
    can't properly set a width smaller than 2x thumb – Zhming Jun 21 '21 at 13:54
7

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"
Asthme
  • 5,163
  • 6
  • 47
  • 65
  • issue is that i do not have colors for background and thumb. i have actual png files. So the png stretches to fit the size of the switch. – Sunny Jul 15 '14 at 20:08
  • @Sunny why you want to use png files..it takes more memory than shape drawables .and its little bit hard to maintain the state of focus...if u r using nine patch,it wont work,use shape instead – Asthme Jul 16 '14 at 06:44
  • I am all for drawing. i could if it is just a simple shape like a rectangle. But here the background has a some icon placed on top of it. I cant draw the icon. so i had to use a png. – Sunny Jul 17 '14 at 21:45
  • @Sunny then calculate the dp,and put png files in all drawables -Xhdpi,hdpi.etc... – Asthme Jul 18 '14 at 07:23
3
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 :)

PoLáKoSz
  • 355
  • 1
  • 6
  • 7
y4n9b0
  • 1,899
  • 1
  • 7
  • 6