How to change switch height and width on Android

  • I wanted to set the switch to 120dp wide and 30dp high.
  • I also want the finger to have no text, but I would like the thumb cover to be half the total width, so I want it to be 60dp.
  • In addition, I want the height of the thumb to be slightly smaller than the height of the background switch that could be seen on three sides.

I have no idea how to make # 3 for both # 1 and # 2, I tried the following xml, but it does not work:

<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" /> 

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

thanks

+7
source share
3 answers

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" 
+18
source

here is a small example of how to change swicth width, I hope this helps someone ...

1) Use cheers 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 the 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) Thumb Selector (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) Track selector (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 the switch

use

  android:switchMinWidth="56dp" android:thumb="@drawable/thumb" android:track="@drawable/track" 
+7
source
 final int switchWidth = Math.max( mSwitchMinWidth, 2 * mThumbWidth + paddingLeft + paddingRight); final int switchHeight = Math.max(trackHeight, thumbHeight); mSwitchWidth = switchWidth; 

The above code is the onMeasure() method in the Switch class, set android:switchMinWidth cannot change the width correctly, the only simple way is to change the mSwitchWidth field by dynamic reflection:

 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(); } 

Remember to setMeasuredDimension(width, getMeasuredHeight()); because the measuredWidth mSwitchWidth switch is not based on the mSwitchWidth field.

I think that changing the height is not difficult, since you know how to change the width :)

0
source

All Articles