BadgeDrawable

public class BadgeDrawable
extends Drawable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ com.google.android.material.badge.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 BottomNavigationView. To create an instance of BadgeDrawable, use create(Context) or ERROR(/#createFromAttributes(Context, AttributeSet, int, int)). How to add and display a BadgeDrawable on top of its anchor view depends on the API level:

For API 18+ (APIs supported by ViewOverlay)

  • Add BadgeDrawable as a ViewOverlay to the desired anchor view using attachBadgeDrawable(BadgeDrawable, View, FrameLayout).
  • Update the BadgeDrawable BadgeDrawable's coordinates (center and bounds) based on its anchor view using updateBadgeCoordinates(View, ViewGroup).
 BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
 BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, null);
 

For Pre API-18

  • Set BadgeDrawable as the foreground of the anchor view's FrameLayout ancestor using attachBadgeDrawable(BadgeDrawable, View, FrameLayout).
  • Update the BadgeDrawable BadgeDrawable's coordinates (center and bounds) based on its anchor view (relative to its FrameLayout ancestor's coordinate space), using updateBadgeCoordinates(View, ViewGroup).
 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(int) to change it to one of the other supported modes.

Note: This is still under development and may not support the full range of customization Material Android components generally support (e.g. themed attributes).

Summary

Nested classes

@interface BadgeDrawable.BadgeGravity

Position the badge can be set to. 

XML attributes

Badge_backgroundColor  
Badge_badgeTextColor  
Badge_maxCharacterCount  
Badge_number  

Constants

int BOTTOM_END

The badge is positioned along the bottom and end edges of its anchor view

int BOTTOM_START

The badge is positioned along the bottom and start edges of its anchor view

int TOP_END

The badge is positioned along the top and end edges of its anchor view

int TOP_START

The badge is positioned along the top and start edges of its anchor view

Public methods

void clearNumber()

Resets any badge number so that a numberless badge will be displayed.

static BadgeDrawable create(Context context)

Creates an instance of BadgeDrawable with default values.

static BadgeDrawable createFromResource(Context context, int id)

Returns a BadgeDrawable from the given XML resource.

void draw(Canvas canvas)
int getAlpha()
int getBackgroundColor()

Returns this badge's background color.

int getBadgeGravity()
int getBadgeTextColor()

Returns this badge's text color.

CharSequence getContentDescription()
int getIntrinsicHeight()

Returns the height at which the badge would like to be laid out.

int getIntrinsicWidth()

Returns the width at which the badge would like to be laid out.

int getMaxCharacterCount()

Returns this badge's max character count.

int getNumber()

Returns this badge's number.

int getOpacity()
BadgeDrawable.SavedState getSavedState()
boolean hasNumber()

Returns whether this badge will display a number.

boolean isStateful()
boolean onStateChange(int[] state)
void setAlpha(int alpha)
void setBackgroundColor(int backgroundColor)

Sets this badge's background color.

void setBadgeGravity(int gravity)

Sets this badge's gravity with respect to its anchor view.

void setBadgeTextColor(int badgeTextColor)

Sets this badge's text color.

void setColorFilter(ColorFilter colorFilter)
void setContentDescriptionNumberless(CharSequence charSequence)
void setContentDescriptionQuantityStringsResource(int stringsResource)
void setMaxCharacterCount(int maxCharacterCount)

Sets this badge's max character count.

void setNumber(int number)

Sets this badge's number.

void setVisible(boolean visible)

Convenience wrapper method for setVisible(boolean, boolean) with the restart parameter hardcoded to false.

void updateBadgeCoordinates(View anchorView, ViewGroup customBadgeParent)

Calculates and updates this badge's center coordinates based on its anchor's bounds.

Inherited methods

From class android.graphics.drawable.Drawable
From class java.lang.Object

XML attributes

Badge_backgroundColor

Related methods:

Badge_badgeTextColor

Related methods:

Badge_maxCharacterCount

Related methods:

Badge_number

Related methods:

Constants

BOTTOM_END

int BOTTOM_END

The badge is positioned along the bottom and end edges of its anchor view

Constant Value: 8388693 (0x00800055)

BOTTOM_START

int BOTTOM_START

The badge is positioned along the bottom and start edges of its anchor view

Constant Value: 8388691 (0x00800053)

TOP_END

int TOP_END

The badge is positioned along the top and end edges of its anchor view

Constant Value: 8388661 (0x00800035)

TOP_START

int TOP_START

The badge is positioned along the top and start edges of its anchor view

Constant Value: 8388659 (0x00800033)

Public methods

clearNumber

void clearNumber ()

Resets any badge number so that a numberless badge will be displayed.

create

BadgeDrawable create (Context context)

Creates an instance of BadgeDrawable with default values.

Parameters
context Context

Returns
BadgeDrawable

createFromResource

BadgeDrawable createFromResource (Context context, 
                int id)

Returns a BadgeDrawable from the given XML resource. All attributes from Badge and a custom style attribute are supported. A badge resource may look like:

<badge
     xmlns:app="http://schemas.android.com/apk/res-auto"
     style="@style/Widget.MaterialComponents.Badge"
     app:maxCharacterCount="2"/>
 

Parameters
context Context

id int

Returns
BadgeDrawable

draw

void draw (Canvas canvas)

Parameters
canvas Canvas

getAlpha

int getAlpha ()

Returns
int

getBackgroundColor

int getBackgroundColor ()

Returns this badge's background color.

Related XML Attributes:

Returns
int

getBadgeGravity

int getBadgeGravity ()

Returns
int

getBadgeTextColor

int getBadgeTextColor ()

Returns this badge's text color.

Related XML Attributes:

Returns
int

getContentDescription

CharSequence getContentDescription ()

Returns
CharSequence

getIntrinsicHeight

int getIntrinsicHeight ()

Returns the height at which the badge would like to be laid out.

Returns
int

getIntrinsicWidth

int getIntrinsicWidth ()

Returns the width at which the badge would like to be laid out.

Returns
int

getMaxCharacterCount

int getMaxCharacterCount ()

Returns this badge's max character count.

Related XML Attributes:

Returns
int

getNumber

int getNumber ()

Returns this badge's number. Only non-negative integer numbers will be returned because the setter clamps negative values to 0.

WARNING: Do not call this method if you are planning to compare to BADGE_NUMBER_NONE

Related XML Attributes:

Returns
int

See also:

getOpacity

int getOpacity ()

Returns
int

getSavedState

BadgeDrawable.SavedState getSavedState ()

Returns
BadgeDrawable.SavedState

hasNumber

boolean hasNumber ()

Returns whether this badge will display a number.

Returns
boolean

isStateful

boolean isStateful ()

Returns
boolean

onStateChange

boolean onStateChange (int[] state)

Parameters
state int

Returns
boolean

setAlpha

void setAlpha (int alpha)

Parameters
alpha int

setBackgroundColor

void setBackgroundColor (int backgroundColor)

Sets this badge's background color.

Related XML Attributes:

Parameters
backgroundColor int: This badge's background color.

setBadgeGravity

void setBadgeGravity (int gravity)

Sets this badge's gravity with respect to its anchor view.

Parameters
gravity int: Constant representing one of 4 possible BadgeDrawable.BadgeGravity values.

setBadgeTextColor

void setBadgeTextColor (int badgeTextColor)

Sets this badge's text color.

Related XML Attributes:

Parameters
badgeTextColor int: This badge's text color.

setColorFilter

void setColorFilter (ColorFilter colorFilter)

Parameters
colorFilter ColorFilter

setContentDescriptionNumberless

void setContentDescriptionNumberless (CharSequence charSequence)

Parameters
charSequence CharSequence

setContentDescriptionQuantityStringsResource

void setContentDescriptionQuantityStringsResource (int stringsResource)

Parameters
stringsResource int

setMaxCharacterCount

void setMaxCharacterCount (int maxCharacterCount)

Sets this badge's max character count.

Related XML Attributes:

Parameters
maxCharacterCount int: This badge's max character count.

setNumber

void setNumber (int number)

Sets this badge's number. Only non-negative integer numbers are supported. If the number is negative, it will be clamped to 0. The specified value will be displayed, unless its number of digits exceeds maxCharacterCount in which case a truncated version will be shown.

Related XML Attributes:

Parameters
number int: This badge's number.

setVisible

void setVisible (boolean visible)

Convenience wrapper method for setVisible(boolean, boolean) with the restart parameter hardcoded to false.

Parameters
visible boolean

updateBadgeCoordinates

void updateBadgeCoordinates (View anchorView, 
                ViewGroup customBadgeParent)

Calculates and updates this badge's center coordinates based on its anchor's bounds. Internally also updates this BadgeDrawable's bounds, because they are dependent on the center coordinates. For pre API-18, coordinates will be calculated relative to customBadgeParent because the BadgeDrawable will be set as the parent's foreground.

Parameters
anchorView View: This badge's anchor.

customBadgeParent ViewGroup: An optional parent view that will set this BadgeDrawable as its foreground.