Skip to content

Commit

Permalink
[Badge] Add getters/setters for vertical and horizontal badge padding
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 520969935
  • Loading branch information
imhappi authored and pekingme committed Mar 31, 2023
1 parent d722a78 commit a0d0b53
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 24 deletions.
26 changes: 14 additions & 12 deletions docs/components/BadgeDrawable.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,20 @@ center, use `setHorizontalOffset(int)` or `setVerticalOffset(int)`

### `BadgeDrawable` Attributes

| Feature | Relevant attributes |
| --------------------- | -----------------------------------------------------------------------------------------------------------------|
| Color | `app:backgroundColor` <br> `app:badgeTextColor` |
| Width | `app:badgeWidth` <br> `app:badgeWithTextWidth` |
| Height | `app:badgeHeight` <br> `app:badgeWithTextHeight` |
| Shape | `app:badgeShapeAppearance` <br> `app:badgeShapeAppearanceOverlay` <br> `app:badgeWithTextShapeAppearance` <br> `app:badgeWithTextShapeAppearanceOverlay` |
| Label | `app:badgeText` (for text) <br> `app:number` (for numbers) |
| Label Length | `app:maxCharacterCount` |
| Label Text Color | `app:badgeTextColor` |
| Label Text Appearance | `app:badgeTextAppearance` |
| Badge Gravity | `app:badgeGravity` |
| Offset Alignment | `app:offsetAlignmentMode` |
| Feature | Relevant attributes |
|-----------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------|
| Color | `app:backgroundColor` <br> `app:badgeTextColor` |
| Width | `app:badgeWidth` <br> `app:badgeWithTextWidth` |
| Height | `app:badgeHeight` <br> `app:badgeWithTextHeight` |
| Shape | `app:badgeShapeAppearance` <br> `app:badgeShapeAppearanceOverlay` <br> `app:badgeWithTextShapeAppearance` <br> `app:badgeWithTextShapeAppearanceOverlay` |
| Label | `app:badgeText` (for text) <br> `app:number` (for numbers) |
| Label Length | `app:maxCharacterCount` |
| Label Text Color | `app:badgeTextColor` |
| Label Text Appearance | `app:badgeTextAppearance` |
| Badge Gravity | `app:badgeGravity` |
| Offset Alignment | `app:offsetAlignmentMode` |
| Horizontal Padding | `app:badgeWidePadding` |
| Vertical Padding | `app:badgeVerticalPadding` |

**Note:** If both `app:badgeText` and `app:number` are specified, the badge label will be `app:badgeText`.

Expand Down
48 changes: 46 additions & 2 deletions lib/java/com/google/android/material/badge/BadgeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,48 @@ private CharSequence getEmptyContentDescription() {
return state.getContentDescriptionNumberless();
}

/**
* Sets how much (in pixels) horizontal padding to add to the badge when it has label contents.
* Note that badges have a minimum width as specified by
* com.google.android.material.R.styleable#Badge_badgeWidth.
*
* @param horizontalPadding badge's horizontal padding
* @attr ref com.google.android.material.R.styleable#Badge_badgeWidePadding
*/
public void setHorizontalPadding(@Px int horizontalPadding) {
if (horizontalPadding != state.getBadgeHorizontalPadding()) {
state.setBadgeHorizontalPadding(horizontalPadding);
updateCenterAndBounds();
}
}

/** Returns the badge horizontal padding in pixels. */
@Px
public int getHorizontalPadding() {
return state.getBadgeHorizontalPadding();
}

/**
* Sets how much (in pixels) vertical padding to add to the badge when it has label contents. Note
* that badges have a minimum height as specified by
* com.google.android.material.R.styleable#Badge_badgeHeight.
*
* @param verticalPadding badge's vertical padding
* @attr ref com.google.android.material.R.styleable#Badge_badgeVerticalPadding
*/
public void setVerticalPadding(@Px int verticalPadding) {
if (verticalPadding != state.getBadgeVerticalPadding()) {
state.setBadgeVerticalPadding(verticalPadding);
updateCenterAndBounds();
}
}

/** Returns the badge vertical padding in pixels. */
@Px
public int getVerticalPadding() {
return state.getBadgeVerticalPadding();
}

/**
* Sets how much (in pixels) to horizontally move this badge towards the center of its anchor.
*
Expand Down Expand Up @@ -1106,7 +1148,8 @@ private void calculateCenterAndBounds(@NonNull Rect anchorRect, @NonNull View an
halfBadgeHeight =
Math.max(
halfBadgeHeight,
textDrawableHelper.getTextHeight(badgeContent) / 2f + state.badgeVerticalPadding);
textDrawableHelper.getTextHeight(badgeContent) / 2f
+ state.getBadgeVerticalPadding());

// If the badge has text, it should at least have the same width as it does height
halfBadgeWidth = Math.max(halfBadgeWidth, halfBadgeHeight);
Expand All @@ -1119,7 +1162,8 @@ private void calculateCenterAndBounds(@NonNull Rect anchorRect, @NonNull View an
halfBadgeWidth =
Math.max(
halfBadgeWidth,
textDrawableHelper.getTextWidth(badgeContent) / 2f + state.badgeWidePadding);
textDrawableHelper.getTextWidth(badgeContent) / 2f
+ state.getBadgeHorizontalPadding());
}

int totalVerticalOffset = getTotalVerticalOffsetForState();
Expand Down
53 changes: 43 additions & 10 deletions lib/java/com/google/android/material/badge/BadgeState.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.PluralsRes;
import androidx.annotation.Px;
import androidx.annotation.RestrictTo;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
Expand Down Expand Up @@ -76,8 +77,6 @@ public final class BadgeState {
final float badgeHeight;
final float badgeWithTextWidth;
final float badgeWithTextHeight;
final float badgeWidePadding;
final float badgeVerticalPadding;
final int horizontalInset;
final int horizontalInsetWithText;

Expand All @@ -102,14 +101,7 @@ public final class BadgeState {
Resources res = context.getResources();
badgeRadius =
a.getDimensionPixelSize(R.styleable.Badge_badgeRadius, BADGE_RADIUS_NOT_SPECIFIED);
badgeWidePadding =
a.getDimensionPixelSize(
R.styleable.Badge_badgeWidePadding,
res.getDimensionPixelSize(R.dimen.mtrl_badge_long_text_horizontal_padding));
badgeVerticalPadding =
a.getDimension(
R.styleable.Badge_badgeVerticalPadding,
res.getDimension(R.dimen.m3_badge_with_text_vertical_padding));

horizontalInset =
context
.getResources()
Expand Down Expand Up @@ -236,6 +228,19 @@ public final class BadgeState {
? a.getInt(R.styleable.Badge_badgeGravity, TOP_END)
: storedState.badgeGravity;

currentState.badgeHorizontalPadding =
storedState.badgeHorizontalPadding == null
? a.getDimensionPixelSize(
R.styleable.Badge_badgeWidePadding,
res.getDimensionPixelSize(R.dimen.mtrl_badge_long_text_horizontal_padding))
: storedState.badgeHorizontalPadding;
currentState.badgeVerticalPadding =
storedState.badgeVerticalPadding == null
? a.getDimensionPixelSize(
R.styleable.Badge_badgeVerticalPadding,
res.getDimensionPixelSize(R.dimen.m3_badge_with_text_vertical_padding))
: storedState.badgeVerticalPadding;

currentState.horizontalOffsetWithoutText =
storedState.horizontalOffsetWithoutText == null
? a.getDimensionPixelOffset(R.styleable.Badge_horizontalOffset, 0)
Expand Down Expand Up @@ -442,6 +447,26 @@ void setBadgeGravity(@BadgeGravity int badgeGravity) {
currentState.badgeGravity = badgeGravity;
}

@Px
int getBadgeHorizontalPadding() {
return currentState.badgeHorizontalPadding;
}

void setBadgeHorizontalPadding(@Px int horizontalPadding) {
overridingState.badgeHorizontalPadding = horizontalPadding;
currentState.badgeHorizontalPadding = horizontalPadding;
}

@Px
int getBadgeVerticalPadding() {
return currentState.badgeVerticalPadding;
}

void setBadgeVerticalPadding(@Px int verticalPadding) {
overridingState.badgeVerticalPadding = verticalPadding;
currentState.badgeVerticalPadding = verticalPadding;
}

@Dimension(unit = Dimension.PX)
int getHorizontalOffsetWithoutText() {
return currentState.horizontalOffsetWithoutText;
Expand Down Expand Up @@ -591,6 +616,10 @@ public static final class State implements Parcelable {
@BadgeGravity private Integer badgeGravity;
private Boolean isVisible = true;

@Px private Integer badgeHorizontalPadding;

@Px private Integer badgeVerticalPadding;

@Dimension(unit = Dimension.PX)
private Integer horizontalOffsetWithoutText;

Expand Down Expand Up @@ -628,6 +657,8 @@ public State() {}
contentDescriptionNumberless = in.readString();
contentDescriptionQuantityStrings = in.readInt();
badgeGravity = (Integer) in.readSerializable();
badgeHorizontalPadding = (Integer) in.readSerializable();
badgeVerticalPadding = (Integer) in.readSerializable();
horizontalOffsetWithoutText = (Integer) in.readSerializable();
verticalOffsetWithoutText = (Integer) in.readSerializable();
horizontalOffsetWithText = (Integer) in.readSerializable();
Expand Down Expand Up @@ -678,6 +709,8 @@ public void writeToParcel(@NonNull Parcel dest, int flags) {
contentDescriptionNumberless != null ? contentDescriptionNumberless.toString() : null);
dest.writeInt(contentDescriptionQuantityStrings);
dest.writeSerializable(badgeGravity);
dest.writeSerializable(badgeHorizontalPadding);
dest.writeSerializable(badgeVerticalPadding);
dest.writeSerializable(horizontalOffsetWithoutText);
dest.writeSerializable(verticalOffsetWithoutText);
dest.writeSerializable(horizontalOffsetWithText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<public name="badgeText" type="attr"/>
<public name="number" type="attr"/>
<public name="badgeGravity" type="attr"/>
<public name="badgeWidePadding" type="attr"/>
<public name="badgeVerticalPadding" type="attr"/>
<public name="horizontalOffset" type="attr"/>
<public name="verticalOffset" type="attr"/>
<public name="offsetAlignmentMode" type="attr"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public class BadgeDrawableTest {
private static final int TEST_BADGE_HORIZONTAL_OFFSET = 10;
private static final int TEST_BADGE_VERTICAL_OFFSET = 5;

private static final int TEST_BADGE_HORIZONTAL_PADDING = 10;
private static final int TEST_BADGE_VERTICAL_PADDING = 10;

private final Context context = ApplicationProvider.getApplicationContext();

@Before
Expand All @@ -70,6 +73,9 @@ public void testSavedState() {
badgeDrawable.setNumber(TEST_BADGE_NUMBER);
badgeDrawable.setBadgeGravity(BadgeDrawable.TOP_START);

badgeDrawable.setHorizontalPadding(TEST_BADGE_HORIZONTAL_PADDING);
badgeDrawable.setVerticalPadding(TEST_BADGE_VERTICAL_PADDING);

badgeDrawable.setHorizontalOffset(TEST_BADGE_HORIZONTAL_OFFSET);
badgeDrawable.setVerticalOffset(TEST_BADGE_VERTICAL_OFFSET);

Expand Down Expand Up @@ -104,6 +110,9 @@ public void testSavedState() {
assertThat(restoredBadgeDrawable.getAlpha()).isEqualTo(255);
assertThat(restoredBadgeDrawable.getMaxCharacterCount()).isEqualTo(4);
assertThat(restoredBadgeDrawable.getBadgeGravity()).isEqualTo(BadgeDrawable.TOP_START);
// badge padding
assertThat(restoredBadgeDrawable.getHorizontalPadding()).isEqualTo(TEST_BADGE_HORIZONTAL_PADDING);
assertThat(restoredBadgeDrawable.getVerticalPadding()).isEqualTo(TEST_BADGE_VERTICAL_PADDING);
// badge offsets
assertThat(restoredBadgeDrawable.getHorizontalOffset()).isEqualTo(TEST_BADGE_HORIZONTAL_OFFSET);
assertThat(restoredBadgeDrawable.getVerticalOffset()).isEqualTo(TEST_BADGE_VERTICAL_OFFSET);
Expand Down

0 comments on commit a0d0b53

Please sign in to comment.