Skip to content

Commit c1ef52b

Browse files
pubiqqpekingme
authored andcommitted
[Badge] Allow text strings in badges
Resolves #2978 GIT_ORIGIN_REV_ID=39c9f60cff607fba1e9ca57ae8bc155c5919e503 Co-authored-by: imhappi PiperOrigin-RevId: 520368686
1 parent 2c23d2a commit c1ef52b

File tree

9 files changed

+231
-66
lines changed

9 files changed

+231
-66
lines changed

docs/components/BadgeDrawable.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,21 @@ center, use `setHorizontalOffset(int)` or `setVerticalOffset(int)`
9696
| Width | `app:badgeWidth` <br> `app:badgeWithTextWidth` |
9797
| Height | `app:badgeHeight` <br> `app:badgeWithTextHeight` |
9898
| Shape | `app:badgeShapeAppearance` <br> `app:badgeShapeAppearanceOverlay` <br> `app:badgeWithTextShapeAppearance` <br> `app:badgeWithTextShapeAppearanceOverlay` |
99-
| Label | `app:number` |
99+
| Label | `app:badgeText` (for text) <br> `app:number` (for numbers) |
100100
| Label Length | `app:maxCharacterCount` |
101101
| Label Text Color | `app:badgeTextColor` |
102102
| Label Text Appearance | `app:badgeTextAppearance` |
103103
| Badge Gravity | `app:badgeGravity` |
104104
| Offset Alignment | `app:offsetAlignmentMode` |
105105

106+
**Note:** If both `app:badgeText` and `app:number` are specified, the badge label will be `app:badgeText`.
107+
106108
### Talkback Support
107109

108-
`BadgeDrawable` provides a getter for its content description, which is based on
109-
the number (if any) displayed. Users should specify content description:
110-
`setContentDescriptionNumberless(CharSequence)`
111-
`setContentDescriptionQuantityStringsResource(@StringRes)`
110+
`BadgeDrawable` provides a getter for its content description, which is based on the displayed
111+
number or text (if any). To specify the content description, the developer is provided
112+
with the following methods:
113+
- `setContentDescriptionForText(CharSequence)`
114+
- `setContentDescriptionQuantityStringsResource(@PluralsRes int)`
115+
- `setContentDescriptionExceedsMaxBadgeNumberStringResource(@StringRes int)`
116+
- `setContentDescriptionNumberless(CharSequence)`

docs/components/BottomNavigation.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ calls to this method will reuse the existing `BadgeDrawable`:
146146
```kt
147147
var badge = bottomNavigation.getOrCreateBadge(menuItemId)
148148
badge.isVisible = true
149-
// An icon only badge will be displayed unless a number is set:
150-
badge.number = 99
149+
// An icon only badge will be displayed unless a number or text is set:
150+
badge.number = 99 // or badge.text = "New"
151151
```
152152

153153
As a best practice, if you need to temporarily hide the badge, for instance
@@ -158,7 +158,7 @@ until the next notification is received, change the visibility of
158158
val badgeDrawable = bottomNavigation.getBadge(menuItemId)
159159
if (badgeDrawable != null) {
160160
badgeDrawable.isVisible = false
161-
badgeDrawable.clearNumber()
161+
badgeDrawable.clearNumber() // or badgeDrawable.clearText()
162162
}
163163
```
164164

docs/components/NavigationRail.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ calls to this method will reuse the existing `BadgeDrawable`:
190190
```kt
191191
var badge = navigationRail.getOrCreateBadge(menuItemId)
192192
badge.isVisible = true
193-
// An icon only badge will be displayed unless a number is set:
194-
badge.number = 99
193+
// An icon only badge will be displayed unless a number or text is set:
194+
badge.number = 99 // or badge.text = "New"
195195
```
196196

197197
As best practice, if you need to temporarily hide the badge, for example until
@@ -201,7 +201,7 @@ the next notification is received, change the visibility of `BadgeDrawable`:
201201
val badgeDrawable = navigationRail.getBadge(menuItemId)
202202
if (badgeDrawable != null) {
203203
badgeDrawable.isVisible = false
204-
badgeDrawable.clearNumber()
204+
badgeDrawable.clearNumber() // or badgeDrawable.clearText()
205205
}
206206
```
207207

docs/components/Tabs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ badge states:
115115

116116
```kt
117117
val badge = tab.getOrCreateBadge()
118+
119+
// For badges with a number
118120
badge.setContentDescriptionNumberless(contentDescription)
119121
badge.setContentDescriptionQuantityStringsResource(R.string.content_description)
120122
badge.setContentDescriptionExceedsMaxBadgeNumberStringResource(R.string.content_description)
123+
124+
// For badges with a text
125+
badge.setContentDescriptionForText(contentDescription)
121126
```
122127

123128
### Using tabs with ViewPager

0 commit comments

Comments
 (0)