Skip to content

Commit 0c2d842

Browse files
Material Design Teamdsn5ft
authored andcommitted
[Button] Add getters and setters for left/right insets to MaterialButton
Currently `MaterialButton` has getters and setters for top/bottom insets, but not for left/right. Which makes it impossible to change left/right insets programmatically at runtime. Note that left/right essentially act like start/end in `MaterialButton` already, since `setPaddingRelative` is used. Keeping consistent here with the naming to avoid confusion. Resolves #1770 Resolves #1790 PiperOrigin-RevId: 897807490
1 parent 49ebf9f commit 0c2d842

2 files changed

Lines changed: 99 additions & 15 deletions

File tree

lib/java/com/google/android/material/button/MaterialButton.java

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ public void setInsetBottom(@Dimension int insetBottom) {
16101610
* Gets the bottom inset for this button
16111611
*
16121612
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetBottom
1613-
* @see #setInsetTop(int)
1613+
* @see #setInsetBottom(int)
16141614
*/
16151615
@Dimension
16161616
public int getInsetBottom() {
@@ -1621,7 +1621,7 @@ public int getInsetBottom() {
16211621
* Sets the button top inset
16221622
*
16231623
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetTop
1624-
* @see #getInsetBottom()
1624+
* @see #getInsetTop()
16251625
*/
16261626
public void setInsetTop(@Dimension int insetTop) {
16271627
materialButtonHelper.setInsetTop(insetTop);
@@ -1638,6 +1638,61 @@ public int getInsetTop() {
16381638
return materialButtonHelper.getInsetTop();
16391639
}
16401640

1641+
/**
1642+
* Sets the button left inset.
1643+
*
1644+
* <p>This acts like the start inset in RTL layouts. It's using the "left" naming convention
1645+
* instead of "start" to be consistent with the android:insetLeft attribute.
1646+
*
1647+
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetLeft
1648+
* @see #getInsetLeft()
1649+
*/
1650+
public void setInsetLeft(@Dimension int insetLeft) {
1651+
materialButtonHelper.setInsetLeft(insetLeft);
1652+
}
1653+
1654+
/**
1655+
* Gets the left inset for this button.
1656+
*
1657+
* <p>This acts like the start inset in RTL layouts. It's using the "left" naming convention
1658+
* instead of "start" to be consistent with the android:insetLeft attribute.
1659+
*
1660+
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetLeft
1661+
* @see #setInsetLeft(int)
1662+
*/
1663+
@Dimension
1664+
public int getInsetLeft() {
1665+
return materialButtonHelper.getInsetLeft();
1666+
}
1667+
1668+
/**
1669+
* Sets the button right inset.
1670+
*
1671+
* <p>This acts like the end inset in RTL layouts. It's using the "right" naming convention
1672+
* instead of "end" to be consistent with the inset to be consistent with the android:insetRight
1673+
* attribute.
1674+
*
1675+
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetRight
1676+
* @see #getInsetRight()
1677+
*/
1678+
public void setInsetRight(@Dimension int insetRight) {
1679+
materialButtonHelper.setInsetRight(insetRight);
1680+
}
1681+
1682+
/**
1683+
* Gets the right inset for this button.
1684+
*
1685+
* <p>This acts like the end inset in RTL layouts. It's using the "right" naming convention
1686+
* instead of "end" to be consistent with the android:insetRight attribute.
1687+
*
1688+
* @attr ref com.google.android.material.R.styleable#MaterialButton_android_insetRight
1689+
* @see #setInsetRight(int)
1690+
*/
1691+
@Dimension
1692+
public int getInsetRight() {
1693+
return materialButtonHelper.getInsetRight();
1694+
}
1695+
16411696
@Override
16421697
protected int[] onCreateDrawableState(int extraSpace) {
16431698
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);

lib/java/com/google/android/material/button/MaterialButtonHelper.java

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ private Drawable createBackground() {
251251
new RippleDrawable(
252252
RippleUtils.sanitizeRippleDrawableColor(rippleColor),
253253
wrapDrawableWithInset(
254-
new LayerDrawable(
255-
new Drawable[] {surfaceColorStrokeDrawable, backgroundDrawable})),
254+
new LayerDrawable(new Drawable[] {surfaceColorStrokeDrawable, backgroundDrawable})),
256255
maskDrawable);
257256
FocusRingDrawable.layer(context, rippleDrawable);
258257
return rippleDrawable;
@@ -467,39 +466,69 @@ ShapeAppearanceModel getShapeAppearanceModel() {
467466
}
468467

469468
public void setInsetBottom(@Dimension int newInsetBottom) {
470-
setVerticalInsets(insetTop, newInsetBottom);
469+
setInsets(insetLeft, insetTop, insetRight, newInsetBottom);
471470
}
472471

473472
public int getInsetBottom() {
474473
return insetBottom;
475474
}
476475

477476
public void setInsetTop(@Dimension int newInsetTop) {
478-
setVerticalInsets(newInsetTop, insetBottom);
477+
setInsets(insetLeft, newInsetTop, insetRight, insetBottom);
479478
}
480479

481-
private void setVerticalInsets(@Dimension int newInsetTop, @Dimension int newInsetBottom) {
480+
public int getInsetTop() {
481+
return insetTop;
482+
}
483+
484+
public void setInsetLeft(@Dimension int newInsetLeft) {
485+
setInsets(newInsetLeft, insetTop, insetRight, insetBottom);
486+
}
487+
488+
public int getInsetLeft() {
489+
return insetLeft;
490+
}
491+
492+
public void setInsetRight(@Dimension int newInsetRight) {
493+
setInsets(insetLeft, insetTop, newInsetRight, insetBottom);
494+
}
495+
496+
public int getInsetRight() {
497+
return insetRight;
498+
}
499+
500+
private void setInsets(
501+
@Dimension int newInsetLeft,
502+
@Dimension int newInsetTop,
503+
@Dimension int newInsetRight,
504+
@Dimension int newInsetBottom) {
505+
482506
// Store padding before setting background, since background overwrites padding values
483507
int paddingStart = materialButton.getPaddingStart();
484508
int paddingTop = materialButton.getPaddingTop();
485509
int paddingEnd = materialButton.getPaddingEnd();
486510
int paddingBottom = materialButton.getPaddingBottom();
511+
512+
int oldInsetLeft = insetLeft;
487513
int oldInsetTop = insetTop;
514+
int oldInsetRight = insetRight;
488515
int oldInsetBottom = insetBottom;
489-
insetBottom = newInsetBottom;
516+
517+
insetLeft = newInsetLeft;
490518
insetTop = newInsetTop;
519+
insetRight = newInsetRight;
520+
insetBottom = newInsetBottom;
521+
491522
if (!backgroundOverwritten) {
492523
updateBackground();
493524
}
494-
// Set the stored padding values
525+
526+
// Set the stored padding values. Left is used as start and right is used as end to be
527+
// consistent with the left/right XML layout attributes.
495528
materialButton.setPaddingRelative(
496-
paddingStart,
529+
paddingStart + newInsetLeft - oldInsetLeft,
497530
paddingTop + newInsetTop - oldInsetTop,
498-
paddingEnd,
531+
paddingEnd + newInsetRight - oldInsetRight,
499532
paddingBottom + newInsetBottom - oldInsetBottom);
500533
}
501-
502-
public int getInsetTop() {
503-
return insetTop;
504-
}
505534
}

0 commit comments

Comments
 (0)