# BadgeDrawable

[View Source](https://github.com/material-components/material-components-android/blob/1.14.0/lib/java/com/google/android/material/badge/BadgeDrawable.java)

*** ** * ** ***


```
public class BadgeDrawable
```

<br />

*** ** * ** ***

`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 `https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView`. To create an instance of `BadgeDrawable`, use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#create(Context)` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#createFromResource(Context,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 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 `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,FrameLayout)`.

```
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 `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,View,FrameLayout)` (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 `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,FrameLayout)`.

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 `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeGravity(int)` to change it to `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()`, the other supported mode. To adjust the badge's offsets w.r.t. the anchor's center, use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setHorizontalOffset(int)`, `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVerticalOffset(int)`

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](https://github.com/material-components/material-components-android/blob/master/docs/components/BadgeDrawable.md) and [design guidelines](https://material.io/components/badges/overview).

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/java/lang/annotation/Retention(value = https://developer.android.com/reference/java/lang/annotation/RetentionPolicy.SOURCE) public annotation https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeGravity` Position the badge can be set to. |

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BADGE_CONTENT_NOT_TRUNCATED() = -2` A value to indicate that badge content should not be truncated. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BADGE_FIXED_EDGE_END() = 1` The badge's edge is fixed at the end and grows towards the start. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BADGE_FIXED_EDGE_START() = 0` The badge's edge is fixed at the start and grows towards the end. |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BOTTOM_END()` **This field is deprecated.** Bottom badge gravities are deprecated in favor of top gravities; use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()` instead. <br /> |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BOTTOM_START()` **This field is deprecated.** Bottom badge gravities are deprecated in favor of top gravities; use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()` instead. <br /> |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()` The badge is positioned along the top and end edges of its anchor view |
| `static final int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` The badge is positioned along the top and start edges of its anchor view |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#clearNumber()()` Clears the badge's number. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#clearText()()` Clears the badge's text. |
| `static https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#create(Context)(Context context)` Creates an instance of `BadgeDrawable` with default values. |
| `static https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#createFromResource(Context,int)(Context context, int id)` Returns a `BadgeDrawable` from the given XML resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#draw(Canvas)(Canvas canvas)` |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getAlpha()()` |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getBackgroundColor()()` Returns this badge's background color. |
| `int` | `@https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeGravity https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getBadgeGravity()()` |
| `https://developer.android.com/reference/java/util/Locale` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getBadgeNumberLocale()()` Returns the `https://developer.android.com/reference/java/util/Locale` used to show badge numbers. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getBadgeTextColor()()` Returns this badge's text color. |
| `https://developer.android.com/reference/java/lang/CharSequence` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getContentDescription()()` |
| `FrameLayout` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getCustomBadgeParent()()` Returns a FrameLayout that will set this `BadgeDrawable` as its foreground. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalOffset()()` Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalOffsetWithText()()` Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor when this badge has text. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalOffsetWithoutText()()` 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). |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalPadding()()` Returns the badge horizontal padding in pixels. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getIntrinsicHeight()()` Returns the height at which the badge would like to be laid out. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getIntrinsicWidth()()` Returns the width at which the badge would like to be laid out. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getLargeFontVerticalOffsetAdjustment()()` 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. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getMaxCharacterCount()()` Returns this badge's max character count. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getMaxNumber()()` Returns this badge's max number. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getNumber()()` Returns the badge's number. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getOpacity()()` |
| `https://developer.android.com/reference/java/lang/String` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getText()()` Returns the badge's text. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalOffset()()` Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalOffsetWithText()()` Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor when the badge has text. |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalOffsetWithoutText()()` 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). |
| `int` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalPadding()()` Returns the badge vertical padding in pixels. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#hasNumber()()` Returns whether this badge will display a number. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#hasText()()` Returns whether the badge will display a text. |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#isStateful()()` |
| `boolean` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#onStateChange(int[])(int[] state)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setAlpha(int)(int alpha)` |
| `void` | `[setAutoAdjustToWithinGrandparentBounds](https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setAutoAdjustToWithinGrandparentBounds(boolean))( boolean autoAdjustToWithinGrandparentBounds )` **This method is deprecated.** Badges now automatically adjust their bounds within the first ancestor view that \* clips its children. <br /> |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBackgroundColor(int)(int backgroundColor)` Sets this badge's background color. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeFixedEdge(int)(int fixedEdge)` Sets this badge's fixed edge. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeGravity(int)(@https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeGravity int gravity)` Sets this badge's gravity with respect to its anchor view. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeNumberLocale(java.util.Locale)(https://developer.android.com/reference/java/util/Locale locale)` Sets the `https://developer.android.com/reference/java/util/Locale` used to show badge numbers. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeTextColor(int)(int badgeTextColor)` Sets this badge's text color. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeWithTextShapeAppearance(int)(int id)` Sets this badge with text's shape appearance resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeWithTextShapeAppearanceOverlay(int)(int id)` Sets this badge with text's shape appearance overlay resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeWithoutTextShapeAppearance(int)(int id)` Sets this badge without text's shape appearance resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeWithoutTextShapeAppearanceOverlay(int)(int id)` Sets this badge without text's shape appearance overlay resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setColorFilter(ColorFilter)(ColorFilter colorFilter)` |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setContentDescriptionExceedsMaxBadgeNumberStringResource(int)( int stringsResource )` Specifies the content description if the badge number exceeds the maximum value. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setContentDescriptionForText(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence charSequence)` Specifies the content description if the text is set for the badge. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setContentDescriptionNumberless(java.lang.CharSequence)(https://developer.android.com/reference/java/lang/CharSequence charSequence)` Specifies the content description if no text or number is set for the badge. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setContentDescriptionQuantityStringsResource(int)(int stringsResource)` Specifies the content description if the number is set for the badge. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setHorizontalOffset(int)(int px)` Sets how much (in pixels) to horizontally move this badge towards the center of its anchor. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setHorizontalOffsetWithText(int)(int px)` Sets how much (in pixels) to horizontally move this badge towards the center of its anchor when this badge has text. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setHorizontalOffsetWithoutText(int)(int px)` 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). |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setHorizontalPadding(int)(int horizontalPadding)` Sets how much (in pixels) horizontal padding to add to the badge when it has label contents. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setLargeFontVerticalOffsetAdjustment(int)(int px)` 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. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setMaxCharacterCount(int)(int maxCharacterCount)` Sets this badge's max character count. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setMaxNumber(int)(int maxNumber)` Sets this badge's max number. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setNumber(int)(int number)` Sets the badge's number. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setText(java.lang.String)(https://developer.android.com/reference/java/lang/String text)` Sets the badge's text. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setTextAppearance(int)(int id)` Sets this badge's text appearance resource. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVerticalOffset(int)(int px)` Sets how much (in pixels) to vertically move this badge towards the center of its anchor. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVerticalOffsetWithText(int)(int px)` Sets how much (in pixels) to vertically move this badge towards the center of its anchor when this badge has text. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVerticalOffsetWithoutText(int)(int px)` 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). |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVerticalPadding(int)(int verticalPadding)` Sets how much (in pixels) vertical padding to add to the badge when it has label contents. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setVisible(boolean)(boolean visible)` Convenience wrapper method for setVisible with the ` restart` parameter hardcoded to false. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View)(View anchorView)` Calculates and updates this badge's center coordinates based on its anchor's bounds. |
| `void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,FrameLayout)(View anchorView, FrameLayout customBadgeParent)` Calculates and updates this badge's center coordinates based on its anchor's bounds. |
| `void` | `[updateBadgeCoordinates](https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,ViewGroup))(View anchorView, ViewGroup customBadgeParent)` **This method is deprecated.** use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,FrameLayout)` instead. <br /> |

## Constants

### BADGE_CONTENT_NOT_TRUNCATED

```
public static final int BADGE_CONTENT_NOT_TRUNCATED = -2
```

A value to indicate that badge content should not be truncated.

### BADGE_FIXED_EDGE_END

```
public static final int BADGE_FIXED_EDGE_END = 1
```

The badge's edge is fixed at the end and grows towards the start.

### BADGE_FIXED_EDGE_START

```
public static final int BADGE_FIXED_EDGE_START = 0
```

The badge's edge is fixed at the start and grows towards the end.

### BOTTOM_END

```
public static final int BOTTOM_END
```

> [!CAUTION]
> **This field is deprecated.**   
>
> Bottom badge gravities are deprecated in favor of top gravities; use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()` instead.

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

### BOTTOM_START

```
public static final int BOTTOM_START
```

> [!CAUTION]
> **This field is deprecated.**   
>
> Bottom badge gravities are deprecated in favor of top gravities; use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()` instead.

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

### TOP_END

```
public static final int TOP_END
```

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

### TOP_START

```
public static final int TOP_START
```

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

## Public methods

### clearNumber

```
public void clearNumber()
```

Clears the badge's number.

### clearText

```
public void clearText()
```

Clears the badge's text.

### create

```
public static BadgeDrawable create(Context context)
```

Creates an instance of `BadgeDrawable` with default values.

### createFromResource

```
public static 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"/>
```

### draw

```
public void draw(Canvas canvas)
```

### getAlpha

```
public int getAlpha()
```

### getBackgroundColor

```
public int getBackgroundColor()
```

Returns this badge's background color.

ref com.google.android.material.R.styleable#Badge_backgroundColor

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBackgroundColor(int)` |   |

### getBadgeGravity

```
@BadgeDrawable.BadgeGravity
public int getBadgeGravity()
```

### getBadgeNumberLocale

```
public Locale getBadgeNumberLocale()
```

Returns the `https://developer.android.com/reference/java/util/Locale` used to show badge numbers.

### getBadgeTextColor

```
public int getBadgeTextColor()
```

Returns this badge's text color.

ref com.google.android.material.R.styleable#Badge_badgeTextColor

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setBadgeTextColor(int)` |   |

### getContentDescription

```
public CharSequence getContentDescription()
```

### getCustomBadgeParent

```
public FrameLayout getCustomBadgeParent()
```

Returns a FrameLayout that will set this `BadgeDrawable` as its foreground.

### getHorizontalOffset

```
public int getHorizontalOffset()
```

Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor.

This returns the horizontal offset for badges without text. If offset for badges with text and without text are different consider using `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalOffsetWithoutText()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getHorizontalOffsetWithText()`.

### getHorizontalOffsetWithText

```
public int getHorizontalOffsetWithText()
```

Returns how much (in pixels) this badge is being horizontally offset towards the center of its anchor when this badge has text.

### getHorizontalOffsetWithoutText

```
public int getHorizontalOffsetWithoutText()
```

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

### getHorizontalPadding

```
public int getHorizontalPadding()
```

Returns the badge horizontal padding in pixels.

### getIntrinsicHeight

```
public int getIntrinsicHeight()
```

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

### getIntrinsicWidth

```
public int getIntrinsicWidth()
```

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

### getLargeFontVerticalOffsetAdjustment

```
public int getLargeFontVerticalOffsetAdjustment()
```

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. Note that this is not the total vertical offset.

### getMaxCharacterCount

```
public int getMaxCharacterCount()
```

Returns this badge's max character count.

ref com.google.android.material.R.styleable#Badge_maxCharacterCount

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setMaxCharacterCount(int)` |   |

### getMaxNumber

```
public int getMaxNumber()
```

Returns this badge's max number. If maxCharacterCount is set, it will override this number.

ref com.google.android.material.R.styleable#Badge_maxNumber

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setMaxNumber(int)` |   |

### getNumber

```
public int getNumber()
```

Returns the 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

ref com.google.android.material.R.styleable#Badge_number

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setNumber(int)` |   |

### getOpacity

```
public int getOpacity()
```

### getText

```
public String getText()
```

Returns the badge's text.

ref com.google.android.material.R.styleable#Badge_badgeText

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setText(java.lang.String)` |   |

### getVerticalOffset

```
public int getVerticalOffset()
```

Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor.

This returns the vertical offset for badges without text. If offset for badges with text and without text are different consider using `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalOffsetWithoutText()` or `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getVerticalOffsetWithText()`.

### getVerticalOffsetWithText

```
public int getVerticalOffsetWithText()
```

Returns how much (in pixels) this badge is being vertically moved towards the center of its anchor when the badge has text.

### getVerticalOffsetWithoutText

```
public int getVerticalOffsetWithoutText()
```

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

### getVerticalPadding

```
public int getVerticalPadding()
```

Returns the badge vertical padding in pixels.

### hasNumber

```
public boolean hasNumber()
```

Returns whether this badge will display a number.

### hasText

```
public boolean hasText()
```

Returns whether the badge will display a text.

### isStateful

```
public boolean isStateful()
```

### onStateChange

```
public boolean onStateChange(int[] state)
```

### setAlpha

```
public void setAlpha(int alpha)
```

### setAutoAdjustToWithinGrandparentBounds

```
public void [setAutoAdjustToWithinGrandparentBounds](https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#setAutoAdjustToWithinGrandparentBounds(boolean))(
    boolean autoAdjustToWithinGrandparentBounds
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Badges now automatically adjust their bounds within the first ancestor view that \* clips its children.

Sets whether or not to auto adjust the badge placement to within the badge anchor's grandparent view.

| Parameters |
|---|---|
| `boolean autoAdjustToWithinGrandparentBounds` | whether or not to auto adjust to within the anchor's grandparent view. |

### setBackgroundColor

```
public void setBackgroundColor(int backgroundColor)
```

Sets this badge's background color.

ref com.google.android.material.R.styleable#Badge_backgroundColor

| Parameters |
|---|---|
| `int backgroundColor` | This badge's background color. |

### setBadgeFixedEdge

```
public void setBadgeFixedEdge(int fixedEdge)
```

Sets this badge's fixed edge. The badge does not grow in the direction of the fixed edge.

| Parameters |
|---|---|
| `int fixedEdge` | Constant representing a `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeFixedEdge` value. The two options are `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BADGE_FIXED_EDGE_START()` and `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#BADGE_FIXED_EDGE_END()`. |

### setBadgeGravity

```
public void setBadgeGravity(@BadgeDrawable.BadgeGravity int gravity)
```

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeGravity int gravity` | Constant representing one of the possible `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable.BadgeGravity` values. There are two recommended gravities: `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_START()` and `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#TOP_END()`. |

### setBadgeNumberLocale

```
public void setBadgeNumberLocale(Locale locale)
```

Sets the `https://developer.android.com/reference/java/util/Locale` used to show badge numbers.

### setBadgeTextColor

```
public void setBadgeTextColor(int badgeTextColor)
```

Sets this badge's text color.

ref com.google.android.material.R.styleable#Badge_badgeTextColor

| Parameters |
|---|---|
| `int badgeTextColor` | This badge's text color. |

### setBadgeWithTextShapeAppearance

```
public void setBadgeWithTextShapeAppearance(int id)
```

Sets this badge with text's shape appearance resource.

ref com.google.android.material.R.styleable#Badge_badgeWithTextShapeAppearance

| Parameters |
|---|---|
| `int id` | This badge's shape appearance res id when there is text. |

### setBadgeWithTextShapeAppearanceOverlay

```
public void setBadgeWithTextShapeAppearanceOverlay(int id)
```

Sets this badge with text's shape appearance overlay resource.

ref com.google.android.material.R.styleable#Badge_badgeWithTextShapeAppearanceOverlay

| Parameters |
|---|---|
| `int id` | This badge's shape appearance overlay res id when there is text. |

### setBadgeWithoutTextShapeAppearance

```
public void setBadgeWithoutTextShapeAppearance(int id)
```

Sets this badge without text's shape appearance resource.

ref com.google.android.material.R.styleable#Badge_badgeShapeAppearance

| Parameters |
|---|---|
| `int id` | This badge's shape appearance res id when there is no text. |

### setBadgeWithoutTextShapeAppearanceOverlay

```
public void setBadgeWithoutTextShapeAppearanceOverlay(int id)
```

Sets this badge without text's shape appearance overlay resource.

ref com.google.android.material.R.styleable#Badge_badgeShapeAppearanceOverlay

| Parameters |
|---|---|
| `int id` | This badge's shape appearance overlay res id when there is no text. |

### setColorFilter

```
public void setColorFilter(ColorFilter colorFilter)
```

### setContentDescriptionExceedsMaxBadgeNumberStringResource

```
public void setContentDescriptionExceedsMaxBadgeNumberStringResource(
    int stringsResource
)
```

Specifies the content description if the badge number exceeds the maximum value.

### setContentDescriptionForText

```
public void setContentDescriptionForText(CharSequence charSequence)
```

Specifies the content description if the text is set for the badge. If the text is set for the badge and the content description is `null`, the badge text will be used as the content description by default.

### setContentDescriptionNumberless

```
public void setContentDescriptionNumberless(CharSequence charSequence)
```

Specifies the content description if no text or number is set for the badge.

### setContentDescriptionQuantityStringsResource

```
public void setContentDescriptionQuantityStringsResource(int stringsResource)
```

Specifies the content description if the number is set for the badge.

### setHorizontalOffset

```
public void setHorizontalOffset(int px)
```

Sets how much (in pixels) to horizontally move this badge towards the center of its anchor.

This sets the horizontal offset for badges without text (dots) and with text.

| Parameters |
|---|---|
| `int px` | badge's horizontal offset |

### setHorizontalOffsetWithText

```
public void setHorizontalOffsetWithText(int px)
```

Sets how much (in pixels) to horizontally move this badge towards the center of its anchor when this badge has text.

| Parameters |
|---|---|
| `int px` | badge's horizontal offset when the badge has text. |

### setHorizontalOffsetWithoutText

```
public void setHorizontalOffsetWithoutText(int px)
```

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

| Parameters |
|---|---|
| `int px` | badge's horizontal offset when the badge does not have text |

### setHorizontalPadding

```
public void setHorizontalPadding(int horizontalPadding)
```

Sets how much (in pixels) horizontal padding to add to the badge when it has label contents. Note that badges have a minimum width as specified by com.google.android.material.R.styleable#Badge_badgeWidth.

ref com.google.android.material.R.styleable#Badge_badgeWidePadding

| Parameters |
|---|---|
| `int horizontalPadding` | badge's horizontal padding |

### setLargeFontVerticalOffsetAdjustment

```
public void setLargeFontVerticalOffsetAdjustment(int px)
```

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. This is in conjunction with the vertical offset with text.

| Parameters |
|---|---|
| `int px` | how much to move the badge's vertical offset away from the center by when the font is large. |

### setMaxCharacterCount

```
public void setMaxCharacterCount(int maxCharacterCount)
```

Sets this badge's max character count.

ref com.google.android.material.R.styleable#Badge_maxCharacterCount

| Parameters |
|---|---|
| `int maxCharacterCount` | This badge's max character count. |

### setMaxNumber

```
public void setMaxNumber(int maxNumber)
```

Sets this badge's max number. If maxCharacterCount is set, it will override this number.

ref com.google.android.material.R.styleable#Badge_maxNumber

| Parameters |
|---|---|
| `int maxNumber` | This badge's max number. |

### setNumber

```
public void setNumber(int number)
```

Sets the 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.

ref com.google.android.material.R.styleable#Badge_number

| Parameters |
|---|---|
| `int number` | This badge's number. |

### setText

```
public void setText(String text)
```

Sets the badge's text. The specified text will be displayed, unless its length exceeds `
maxCharacterCount` in which case a truncated version will be shown.

ref com.google.android.material.R.styleable#Badge_badgeText

| See also |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#getText()` |   |

### setTextAppearance

```
public void setTextAppearance(int id)
```

Sets this badge's text appearance resource.

ref com.google.android.material.R.styleable#Badge_badgeTextAppearance

| Parameters |
|---|---|
| `int id` | This badge's text appearance res id. |

### setVerticalOffset

```
public void setVerticalOffset(int px)
```

Sets how much (in pixels) to vertically move this badge towards the center of its anchor.

This sets the vertical offset for badges both without text (dots) and with text.

| Parameters |
|---|---|
| `int px` | badge's vertical offset |

### setVerticalOffsetWithText

```
public void setVerticalOffsetWithText(int px)
```

Sets how much (in pixels) to vertically move this badge towards the center of its anchor when this badge has text.

| Parameters |
|---|---|
| `int px` | badge's vertical offset when the badge has text. |

### setVerticalOffsetWithoutText

```
public void setVerticalOffsetWithoutText(int px)
```

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

| Parameters |
|---|---|
| `int px` | badge's vertical offset when the badge does not have text |

### setVerticalPadding

```
public void setVerticalPadding(int verticalPadding)
```

Sets how much (in pixels) vertical padding to add to the badge when it has label contents. Note that badges have a minimum height as specified by com.google.android.material.R.styleable#Badge_badgeHeight.

ref com.google.android.material.R.styleable#Badge_badgeVerticalPadding

| Parameters |
|---|---|
| `int verticalPadding` | badge's vertical padding |

### setVisible

```
public void setVisible(boolean visible)
```

Convenience wrapper method for setVisible with the `
restart` parameter hardcoded to false.

### updateBadgeCoordinates

```
public void updateBadgeCoordinates(View anchorView)
```

Calculates and updates this badge's center coordinates based on its anchor's bounds. Internally also updates this `BadgeDrawable BadgeDrawable's` bounds, because they are dependent on the center coordinates.

| Parameters |
|---|---|
| `View anchorView` | This badge's anchor. |

### updateBadgeCoordinates

```
public void updateBadgeCoordinates(View anchorView, FrameLayout customBadgeParent)
```

Calculates and updates this badge's center coordinates based on its anchor's bounds. Internally also updates this `BadgeDrawable BadgeDrawable's` bounds, because they are dependent on the center coordinates.

| Parameters |
|---|---|
| `View anchorView` | This badge's anchor. |
| `FrameLayout customBadgeParent` | An optional parent view that will set this `BadgeDrawable` as its foreground. |

### updateBadgeCoordinates

```
public void [updateBadgeCoordinates](https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,ViewGroup))(View anchorView, ViewGroup customBadgeParent)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> use `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable#updateBadgeCoordinates(View,FrameLayout)` instead.

Calculates and updates this badge's center coordinates based on its anchor's bounds. Internally also updates this `BadgeDrawable 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 |
|---|---|
| `View anchorView` | This badge's anchor. |
| `ViewGroup customBadgeParent` | An optional parent view that will set this `BadgeDrawable` as its foreground. |