Skip to content

Commit 058dd07

Browse files
wcshildjcmu
authored andcommitted
Add BadgeDrawable documentation. Update Bottom Navigation documentation to include information about badges.
PiperOrigin-RevId: 246309667
1 parent 71f0a71 commit 058dd07

File tree

6 files changed

+123
-16
lines changed

6 files changed

+123
-16
lines changed

docs/components/BadgeDrawable.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!--docs:
2+
title: "Badge"
3+
layout: detail
4+
section: components
5+
excerpt: "Badges can contain dynamic information, such as a number of pending requests."
6+
iconId: badge
7+
path: /catalog/bottom-navigation-view/
8+
-->
9+
10+
# BadgeDrawable
11+
12+
<!--*
13+
# Document freshness: For more information, see go/fresh-source.
14+
freshness: { owner: 'connieshi' reviewed: '2019-05-01' }
15+
*-->
16+
17+
Badge | Badge with number | Badge with a maximum character count
18+
--------------------------------------- | ---------------------------------------------- | ------------------------------------
19+
![badge_icon](assets/IconOnlyBadge.png) | ![badge_with_number_8](assets/BadgeNumber.png) | ![badge_with_999+](assets/BadgeNumberLongerThanMaxCharCount.png)
20+
21+
A `BadgeDrawable` represents dynamic information such as a number of pending
22+
requests in a [`BottomNavigationView`](BottomNavigationView.md) or
23+
[`TabLayout`](TabLayout.md).
24+
25+
## Design & API Documentation
26+
27+
- [Material Design guidelines: Chips](https://material.io/design/components/bottom-navigation.html#behavior)
28+
<!--{: .icon-list-item.icon-list-item--spec }-->
29+
- [Class definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/badge/BadgeDrawable.java)
30+
<!--{: .icon-list-item.icon-list-item--link }-->
31+
<!-- TODO: Add class overview once the javadoc is published on developer.android.com -->
32+
<!-- - [Class overview](https://developer.android.com/reference/com/google/android/material/badge/Badge) -->
33+
<!--{: .icon-list-item.icon-list-item--link }--> <!--{: .icon-list }-->
34+
35+
## Usage
36+
37+
Create an instance of `BadgeDrawable` by calling `create(Context)` or
38+
`createFromAttributes(Context, AttributeSet, int, int)}`.
39+
40+
How to add and display a `BadgeDrawable` on top of its anchor view depends on
41+
the API level:
42+
43+
In API 18+ (APIs supported by
44+
[ViewOverlay](https://developer.android.com/reference/android/view/ViewOverlay))
45+
46+
1. Add `BadgeDrawable` as a
47+
[ViewOverlay](https://developer.android.com/reference/android/view/ViewOverlay)
48+
to the desired anchor view.
49+
1. Update the `BadgeDrawable`'s coordinates (center and bounds) based on its
50+
anchor view.
51+
52+
Both of the above steps have been encapsulated in a util method:
53+
54+
```java
55+
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, null);
56+
```
57+
58+
In Pre API-18
59+
60+
1. Set `BadgeDrawable` as the foreground of the anchor view's FrameLayout
61+
ancestor.
62+
1. Update the `BadgeDrawable`'s coordinates (center and bounds) based on its
63+
anchor view (relative to its FrameLayout ancestor's coordinate space),
64+
65+
Both of the above steps have been encapsulated in a util method:
66+
67+
```java
68+
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, anchorFrameLayoutParent);
69+
```
70+
71+
### BadgeDrawable Attributes
72+
73+
Feature | Relevant attributes
74+
------------ | -----------------------------------------------
75+
Color | `app:backgroundColor` <br> `app:badgeTextColor`
76+
Label | `app:number`
77+
Label Length | `app:maxCharacterCount`
78+
79+
### Talkback Support
80+
81+
`BadgeDrawable` provides a getter for its content description, which is based on
82+
the number (if any) being displayed. Users should specify content description:
83+
`setContentDescriptionNumberless(CharSequence)`
84+
`setContentDescriptionQuantityStringsResource(@StringRes)`

docs/components/BottomNavigationView.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,25 @@ navigate to.
2020

2121
## Design & API Documentation
2222

23-
- [Material Design guidelines: Bottom
24-
Navigation](https://material.io/go/design-bottom-navigation)
23+
- [Material Design guidelines: Bottom Navigation](https://material.io/go/design-bottom-navigation)
2524
<!--{: .icon-list-item.icon-list-item--spec }-->
26-
- [Class
27-
definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/bottomnavigation/BottomNavigationView.java)
25+
- [Class definition](https://github.com/material-components/material-components-android/tree/master/lib/java/com/google/android/material/bottomnavigation/BottomNavigationView.java)
2826
<!--{: .icon-list-item.icon-list-item--link }-->
2927
<!-- Styles for list items requiring icons instead of standard bullets. -->
30-
- [Class
31-
overview](https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView)
28+
- [Class overview](https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView)
3229
<!--{: .icon-list-item.icon-list-item--link }--> <!--{: .icon-list }-->
3330

3431
## Usage
3532

36-
1. Create a [menu
37-
resource](https://developer.android.com/guide/topics/resources/menu-resource.html)
38-
with up to 5 navigation targets (`BottomNavigationView` does not support more than
39-
5 items).
40-
2. Lay out your `BottomNavigationView` below your content.
41-
3. Set the `app:menu` attribute on your `BottomNavigationView` to your menu
42-
resource.
43-
4. Listen for selection events using `setOnNavigationItemSelectedListener(...)`.
33+
1. Create a
34+
[menu resource](https://developer.android.com/guide/topics/resources/menu-resource.html)
35+
with up to 5 navigation targets (`BottomNavigationView` does not support
36+
more than 5 items).
37+
2. Lay out your `BottomNavigationView` below your content.
38+
3. Set the `app:menu` attribute on your `BottomNavigationView` to your menu
39+
resource.
40+
4. Listen for selection events using
41+
`setOnNavigationItemSelectedListener(...)`.
4442

4543
A typical layout file would look something like this:
4644

@@ -66,6 +64,31 @@ A typical layout file would look something like this:
6664
</FrameLayout>
6765
```
6866

67+
### Badges
68+
69+
![Bottom Navigation with badges](assets/bottom-navigation-badges.png)
70+
71+
`BottomNavigationView` supports displaying icon and number badges.
72+
73+
Initializes and shows a BadgeDrawable associated with menuItemId. Subsequent
74+
calls to this method will reuse the existing BadgeDrawable.
75+
76+
```java
77+
BadgeDrawable badge = bottomNavigationView.showBadge(menuItemId);
78+
```
79+
NOTE: Don't forget to remove any BadgeDrawables that are no longer needed.
80+
81+
```java
82+
bottomNavigationView.removeBadge(menuItemId);
83+
```
84+
85+
Best Practice: If you only need to temporarily hide the badge(e.g. until the next
86+
notification is received), the recommended/lightweight alternative is to change
87+
the visibility of the BadgeDrawable instead.
88+
89+
Please see [`BadgeDrawable`](BadgeDrawable.md) for details on how to update the
90+
badge content being displayed.
91+
6992
### Handling States
7093

7194
The `app:itemIconTint` and `app:itemTextColor` take a
@@ -191,8 +214,8 @@ theming attributes.
191214
There are other navigation patterns you should be aware of:
192215

193216
- [Hierarchical navigation](https://developer.android.com/training/implementing-navigation/index.html).
194-
*See also [Navigation with Back and
195-
Up](https://developer.android.com/design/patterns/navigation.html).*
217+
*See also
218+
[Navigation with Back and Up](https://developer.android.com/design/patterns/navigation.html).*
196219
- Swipeable tabs using [TabLayout](TabLayout.md) and
197220
[ViewPager](https://developer.android.com/reference/android/support/v4/view/ViewPager.html).
198221
- Using [NavigationView](NavigationView.md) to display a longer list of
1.01 KB
Loading
1.19 KB
Loading
505 Bytes
Loading
13.6 KB
Loading

0 commit comments

Comments
 (0)