Badge Drawable
BadgeDrawable contains all the layout and draw logic for a badge.
You can use BadgeDrawable to display dynamic information such as a number of pending requests in a com.google.android.material.bottomnavigation.BottomNavigationView. To create an instance of BadgeDrawable, use create or createFromResource. How to add and display a BadgeDrawable on top of its anchor view depends on the API level:
For API 18+ (APIs supported by android.view.ViewOverlay)
- Add
BadgeDrawableas a android.view.ViewOverlay to the desired anchor view using BadgeUtils#attachBadgeDrawable(BadgeDrawable, View, FrameLayout) (This helper class is currently experimental). - Update the
BadgeDrawable BadgeDrawable'scoordinates (center and bounds) based on its anchor view using updateBadgeCoordinates.
BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
badgeDrawable.setVisible(true);
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor);
For Pre API-18
- Set
BadgeDrawableas the foreground of the anchor view'sFrameLayoutancestor using attachBadgeDrawable (This helper class is currently experimental). - Update the
BadgeDrawable BadgeDrawable'scoordinates (center and bounds) based on its anchor view (relative to itsFrameLayoutancestor's coordinate space), using updateBadgeCoordinates.
BadgeDrawable will dynamically create and wrap the anchor view in a
FrameLayout, then insert the FrameLayout into the anchor view original position in the view hierarchy. Same syntax as API 18+ BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
badgeDrawable.setVisible(true);
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor);
BadgeDrawable to modify your view hierarchy, you can specify a FrameLayout to display the badge instead. BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, anchorFrameLayoutParent);
By default, BadgeDrawable is aligned to the top and end edges of its anchor view (with some offsets). Call setBadgeGravity to change it to TOP_START, the other supported mode. To adjust the badge's offsets w.r.t. the anchor's center, use setHorizontalOffset, setVerticalOffset
Note: This is still under development and may not support the full range of customization Material Android components generally support (e.g. themed attributes).
For more information, see the component developer guidance and design guidelines.
Properties
Functions
BadgeDrawable from the given XML resource.BadgeDrawable as its foreground.
restart parameter hardcoded to false.