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 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);
 

Summary

Nested classes

class BadgeDrawable.SavedState

A Parcelable implementation used to ensure the state of BadgeDrawable is saved. 

XML attributes

Badge_backgroundColor  
Badge_badgeTextColor  
Badge_maxCharacterCount  
Badge_number  

Public methods

void clearBadgeNumber()

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 createFromAttributes(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Returns a BadgeDrawable from the given attributes.

static BadgeDrawable createFromSavedState(Context context, BadgeDrawable.SavedState savedState)

Creates an instance of BadgeDrawable with the provided BadgeDrawable.SavedState.

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

Returns this badge's background color.

int getBadgeTextColor()

Returns this badge's text color.

CharSequence getContentDescription(Context context)
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 onTextSizeChange()
void setAlpha(int alpha)
void setBackgroundColor(int backgroundColor)

Sets this badge's background color.

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 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:

Public methods

clearBadgeNumber

void clearBadgeNumber ()

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

createFromAttributes

BadgeDrawable createFromAttributes (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Returns a BadgeDrawable from the given attributes.

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Returns
BadgeDrawable

createFromSavedState

BadgeDrawable createFromSavedState (Context context, 
                BadgeDrawable.SavedState savedState)

Creates an instance of BadgeDrawable with the provided BadgeDrawable.SavedState.

Parameters
context Context

savedState BadgeDrawable.SavedState

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

getBadgeTextColor

int getBadgeTextColor ()

Returns this badge's text color.

Related XML Attributes:

Returns
int

getContentDescription

CharSequence getContentDescription (Context context)

Parameters
context Context

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

onTextSizeChange

void onTextSizeChange ()

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.

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.

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.