I want to reduce my Switch's width. The current solution in How to change the size of a Switch Widget and How to change Width of Android's Switch track? is not solving my problem. android:switchMinWidth
just defined the minWidth, but I want it to be smaller than 2 x thumbWidth
size.
I dig into the SwitchCompat
class' onMeasure()
function. The way the width is defined is as below.
// Adjust left and right padding to ensure there's enough room for the
// thumb's padding (when present).
int paddingLeft = padding.left;
int paddingRight = padding.right;
if (mThumbDrawable != null) {
final Rect inset = DrawableUtils.getOpticalBounds(mThumbDrawable);
paddingLeft = Math.max(paddingLeft, inset.left);
paddingRight = Math.max(paddingRight, inset.right);
}
final int switchWidth = Math.max(mSwitchMinWidth,
2 * mThumbWidth + paddingLeft + paddingRight);
final int switchHeight = Math.max(trackHeight, thumbHeight);
mSwitchWidth = switchWidth;
mSwitchHeight = switchHeight;
I was considering using negative padding, but then there's this line paddingLeft = Math.max(paddingLeft, inset.left);
. I'm not sure how to set the inset
to my thumb drawable that has negative value (I don't know what inset
or OpticalBounds
is. Maybe this should be another stackoverflow question ).
Anyone has idea how I could shrink the width of my SwitchCompat?
Update I have filed a request to google on this https://code.google.com/p/android/issues/detail?id=227184&thanks=227184&ts=1478485498