BadgeDrawable

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 BadgeDrawable as 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's coordinates (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 BadgeDrawable as the foreground of the anchor view's FrameLayout ancestor using attachBadgeDrawable (This helper class is currently experimental).
  • Update the BadgeDrawable BadgeDrawable's coordinates (center and bounds) based on its anchor view (relative to its FrameLayout ancestor's coordinate space), using updateBadgeCoordinates.
Option 1: 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);
Option 2: If you do not want 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.

Types

Link copied to clipboard
Position the badge can be set to.

Properties

Link copied to clipboard
A value to indicate that badge content should not be truncated.
Link copied to clipboard
val BOTTOM_END: Int = 8388693
The badge is positioned along the bottom and end edges of its anchor view
Link copied to clipboard
val BOTTOM_START: Int = 8388691
The badge is positioned along the bottom and start edges of its anchor view
Link copied to clipboard
val TOP_END: Int = 8388661
The badge is positioned along the top and end edges of its anchor view
Link copied to clipboard
val TOP_START: Int = 8388659
The badge is positioned along the top and start edges of its anchor view

Functions

Link copied to clipboard
open fun clearNumber()
Clears the badge's number.
Link copied to clipboard
open fun clearText()
Clears the badge's text.
Link copied to clipboard
Creates an instance of BadgeDrawable with default values.
Link copied to clipboard
Returns a BadgeDrawable from the given XML resource.
Link copied to clipboard
open fun draw(@NonNull canvas: Canvas)
Link copied to clipboard
open fun getAlpha(): Int
Link copied to clipboard
Returns this badge's background color.
Link copied to clipboard
open fun getBadgeGravity(): Int
Link copied to clipboard
Returns the Locale used to show badge numbers.
Link copied to clipboard
Returns this badge's text color.
Link copied to clipboard
Link copied to clipboard
Returns a FrameLayout that will set this BadgeDrawable as its foreground.
Link copied to clipboard
Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor.
Link copied to clipboard
Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor when this badge does not have text (is a dot).
Link copied to clipboard
Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor when this badge has text.
Link copied to clipboard
Returns the badge horizontal padding in pixels.
Link copied to clipboard
Returns the height at which the badge would like to be laid out.
Link copied to clipboard
Returns the width at which the badge would like to be laid out.
Link copied to clipboard
Returns how much (in pixels) this badge is being vertically moved away the center of its anchor when the badge has text and the font scale is at max.
Link copied to clipboard
Returns this badge's max character count.
Link copied to clipboard
open fun getMaxNumber(): Int
Returns this badge's max number.
Link copied to clipboard
open fun getNumber(): Int
Returns the badge's number.
Link copied to clipboard
open fun getOpacity(): Int
Link copied to clipboard
open fun getText(): String
Returns the badge's text.
Link copied to clipboard
Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor.
Link copied to clipboard
Returns how much (in pixels) this badge is being vertically offset towards the center of its anchor when this badge does not have text (is a dot).
Link copied to clipboard
Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor when the badge has text.
Link copied to clipboard
Returns the badge vertical padding in pixels.
Link copied to clipboard
open fun hasNumber(): Boolean
Returns whether this badge will display a number.
Link copied to clipboard
open fun hasText(): Boolean
Returns whether the badge will display a text.
Link copied to clipboard
open fun isStateful(): Boolean
Link copied to clipboard
open fun onStateChange(state: Array<Int>): Boolean
Link copied to clipboard
Implements the TextDrawableHelper.TextDrawableDelegate interface.
Link copied to clipboard
open fun setAlpha(alpha: Int)
Link copied to clipboard
open fun setAutoAdjustToWithinGrandparentBounds(autoAdjustToWithinGrandparentBounds: Boolean)
Sets whether or not to auto adjust the badge placement to within the badge anchor's grandparent view.
Link copied to clipboard
open fun setBackgroundColor(@ColorInt backgroundColor: Int)
Sets this badge's background color.
Link copied to clipboard
open fun setBadgeGravity(gravity: Int)
Sets this badge's gravity with respect to its anchor view.
Link copied to clipboard
open fun setBadgeNumberLocale(@NonNull locale: Locale)
Sets the Locale used to show badge numbers.
Link copied to clipboard
open fun setBadgeTextColor(@ColorInt badgeTextColor: Int)
Sets this badge's text color.
Link copied to clipboard
Sets this badge without text's shape appearance resource.
Sets this badge without text's shape appearance overlay resource.
Link copied to clipboard
Sets this badge with text's shape appearance resource.
Link copied to clipboard
Sets this badge with text's shape appearance overlay resource.
Link copied to clipboard
open fun setColorFilter(colorFilter: ColorFilter)
Specifies the content description if the badge number exceeds the maximum value.
Link copied to clipboard
Specifies the content description if the text is set for the badge.
Link copied to clipboard
Specifies the content description if no text or number is set for the badge.
Specifies the content description if the number is set for the badge.
Link copied to clipboard
open fun setHorizontalOffset(px: Int)
Sets how much (in pixels) to horizontally move this badge towards the center of its anchor.
Link copied to clipboard
Sets how much (in pixels) to horizontally move this badge towards the center of its anchor when this badge does not have text (is a dot).
Link copied to clipboard
Sets how much (in pixels) to horizontally move this badge towards the center of its anchor when this badge has text.
Link copied to clipboard
open fun setHorizontalPadding(@Px horizontalPadding: Int)
Sets how much (in pixels) horizontal padding to add to the badge when it has label contents.
Link copied to clipboard
Sets how much (in pixels) to vertically move this badge away the center of its anchor when this badge has text and the font scale is at max size.
Link copied to clipboard
open fun setMaxCharacterCount(maxCharacterCount: Int)
Sets this badge's max character count.
Link copied to clipboard
open fun setMaxNumber(maxNumber: Int)
Sets this badge's max number.
Link copied to clipboard
open fun setNumber(number: Int)
Sets the badge's number.
Link copied to clipboard
open fun setText(@Nullable text: String)
Sets the badge's text.
Link copied to clipboard
Sets this badge's text appearance resource.
Link copied to clipboard
open fun setVerticalOffset(px: Int)
Sets how much (in pixels) to vertically move this badge towards the center of its anchor.
Link copied to clipboard
Sets how much (in pixels) to vertically move this badge towards the center of its anchor when this badge does not have text (is a dot).
Link copied to clipboard
Sets how much (in pixels) to vertically move this badge towards the center of its anchor when this badge has text.
Link copied to clipboard
open fun setVerticalPadding(@Px verticalPadding: Int)
Sets how much (in pixels) vertical padding to add to the badge when it has label contents.
Link copied to clipboard
open fun setVisible(visible: Boolean)
Convenience wrapper method for setVisible with the restart parameter hardcoded to false.
Link copied to clipboard
open fun updateBadgeCoordinates(@NonNull anchorView: View)
open fun updateBadgeCoordinates(@NonNull anchorView: View, @Nullable customBadgeParent: ViewGroup)
open fun updateBadgeCoordinates(@NonNull anchorView: View, @Nullable customBadgeParent: FrameLayout)
Calculates and updates this badge's center coordinates based on its anchor's bounds.