# BadgeUtils

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

*** ** * ** ***


```
@ExperimentalBadgeUtils
public class BadgeUtils
```

<br />

*** ** * ** ***

Utility class for `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable`.

Warning: This class is experimental and the APIs are subject to change.

## Summary

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,View)(https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, View anchor)` |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,View,FrameLayout)( https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, View anchor, FrameLayout customBadgeParent )` Attaches a BadgeDrawable to its associated anchor and update the BadgeDrawable's coordinates based on the anchor. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,Toolbar,int)( https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, Toolbar toolbar, int menuItemId )` A convenience method to attach a BadgeDrawable to the specified menu item on a toolbar, update the BadgeDrawable's coordinates based on its anchor and adjust the BadgeDrawable's offset so it is not clipped off by the toolbar. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#attachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,Toolbar,int,FrameLayout)( https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, Toolbar toolbar, int menuItemId, FrameLayout customBadgeParent )` Attaches a BadgeDrawable to its associated action menu item on a toolbar, update the BadgeDrawable's coordinates based on this anchor and adjust the BadgeDrawable's offset so it is not clipped off by the toolbar. |
| `static SparseArray<BadgeDrawable>` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#createBadgeDrawablesFromSavedStates(Context,com.google.android.material.internal.ParcelableSparseArray)( Context context, https://developer.android.com/reference/com/google/android/material/internal/ParcelableSparseArray badgeStates )` Given a map of int keys to `https://developer.android.com/reference/com/google/android/material/badge/BadgeState.State`, creates a parcelable map of int keys to `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable`. |
| `static https://developer.android.com/reference/com/google/android/material/internal/ParcelableSparseArray` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#createParcelableBadgeStates(SparseArray<BadgeDrawable>)(SparseArray<BadgeDrawable> badgeDrawables)` Given a map of int keys to `BadgeDrawable BadgeDrawables`, creates a parcelable map of unique int keys to `BadgeDrawable.SavedState SavedStates`. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#detachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,View)(https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, View anchor)` Detaches a BadgeDrawable from its associated anchor. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#detachBadgeDrawable(com.google.android.material.badge.BadgeDrawable,Toolbar,int)( https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, Toolbar toolbar, int menuItemId )` Detaches a BadgeDrawable from its associated action menu item on a toolbar, The BadgeDrawable will be removed from its anchor's ViewOverlay. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#setBadgeDrawableBounds(com.google.android.material.badge.BadgeDrawable,View,FrameLayout)( https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable badgeDrawable, View anchor, FrameLayout compatBadgeParent )` Sets the bounds of a BadgeDrawable to match the bounds of its anchor or its anchor's FrameLayout ancestor if it has a custom parent set. |
| `static void` | `https://developer.android.com/reference/com/google/android/material/badge/BadgeUtils#updateBadgeBounds(Rect,float,float,float,float)( Rect rect, float centerX, float centerY, float halfWidth, float halfHeight )` Updates a badge's bounds using its center coordinate, `halfWidth` and `halfHeight`. |

## Public methods

### attachBadgeDrawable

```
public static void attachBadgeDrawable(BadgeDrawable badgeDrawable, View anchor)
```

### attachBadgeDrawable

```
public static void attachBadgeDrawable(
    BadgeDrawable badgeDrawable,
    View anchor,
    FrameLayout customBadgeParent
)
```

Attaches a BadgeDrawable to its associated anchor and update the BadgeDrawable's coordinates based on the anchor. The BadgeDrawable will be added as a view overlay as default. If it has a FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable will be set as the foreground of that.

### attachBadgeDrawable

```
public static void attachBadgeDrawable(
    BadgeDrawable badgeDrawable,
    Toolbar toolbar,
    int menuItemId
)
```

A convenience method to attach a BadgeDrawable to the specified menu item on a toolbar, update the BadgeDrawable's coordinates based on its anchor and adjust the BadgeDrawable's offset so it is not clipped off by the toolbar.

Menu item views are reused by the menu, so any structural changes to the menu may require detaching the BadgeDrawable and re-attaching it to the correct item.

### attachBadgeDrawable

```
public static void attachBadgeDrawable(
    BadgeDrawable badgeDrawable,
    Toolbar toolbar,
    int menuItemId,
    FrameLayout customBadgeParent
)
```

Attaches a BadgeDrawable to its associated action menu item on a toolbar, update the BadgeDrawable's coordinates based on this anchor and adjust the BadgeDrawable's offset so it is not clipped off by the toolbar. The BadgeDrawable will be added as a view overlay as default. If it has a FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable will be set as the foreground of that.

Menu item views are reused by the menu, so any structural changes to the menu may require detaching the BadgeDrawable and re-attaching it to the correct item.

### createBadgeDrawablesFromSavedStates

```
public static SparseArray<BadgeDrawable> createBadgeDrawablesFromSavedStates(
    Context context,
    ParcelableSparseArray badgeStates
)
```

Given a map of int keys to `https://developer.android.com/reference/com/google/android/material/badge/BadgeState.State`, creates a parcelable map of int keys to `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable`. Useful for state restoration.

| Parameters |
|---|---|
| `Context context` | Current context |
| `https://developer.android.com/reference/com/google/android/material/internal/ParcelableSparseArray badgeStates` | A parcelable SparseArray that contains a map of int keys (e.g. menuItemId) to `https://developer.android.com/reference/com/google/android/material/badge/BadgeState.State`. |

| Returns |
|---|---|
| `SparseArray<BadgeDrawable>` | A SparseArray that contains a map of int keys (e.g. menuItemId) to `https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable`. |

### createParcelableBadgeStates

```
public static ParcelableSparseArray createParcelableBadgeStates(SparseArray<BadgeDrawable> badgeDrawables)
```

Given a map of int keys to `BadgeDrawable BadgeDrawables`, creates a parcelable map of unique int keys to `BadgeDrawable.SavedState SavedStates`. Useful for state restoration.

| Parameters |
|---|---|
| `SparseArray<BadgeDrawable> badgeDrawables` | A SparseArray that contains a map of int keys (e.g. menuItemId) to `BadgeDrawable BadgeDrawables`. |

| Returns |
|---|---|
| `https://developer.android.com/reference/com/google/android/material/internal/ParcelableSparseArray` | A parcelable SparseArray that contains a map of int keys (e.g. menuItemId) to `BadgeDrawable.SavedState SavedStates`. |

### detachBadgeDrawable

```
public static void detachBadgeDrawable(BadgeDrawable badgeDrawable, View anchor)
```

Detaches a BadgeDrawable from its associated anchor. The BadgeDrawable will be removed from its anchor's ViewOverlay. If it has a FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable will be removed from the parent's foreground instead.

### detachBadgeDrawable

```
public static void detachBadgeDrawable(
    BadgeDrawable badgeDrawable,
    Toolbar toolbar,
    int menuItemId
)
```

Detaches a BadgeDrawable from its associated action menu item on a toolbar, The BadgeDrawable will be removed from its anchor's ViewOverlay. If it has a FrameLayout custom parent that is an ancestor of the anchor, then the BadgeDrawable will be removed from the parent's foreground instead.

### setBadgeDrawableBounds

```
public static void setBadgeDrawableBounds(
    BadgeDrawable badgeDrawable,
    View anchor,
    FrameLayout compatBadgeParent
)
```

Sets the bounds of a BadgeDrawable to match the bounds of its anchor or its anchor's FrameLayout ancestor if it has a custom parent set.

### updateBadgeBounds

```
public static void updateBadgeBounds(
    Rect rect,
    float centerX,
    float centerY,
    float halfWidth,
    float halfHeight
)
```

Updates a badge's bounds using its center coordinate, `halfWidth` and `halfHeight`.

| Parameters |
|---|---|
| `Rect rect` | Holds rectangular coordinates of the badge's bounds. |
| `float centerX` | A badge's center x coordinate. |
| `float centerY` | A badge's center y coordinate. |
| `float halfWidth` | Half of a badge's width. |
| `float halfHeight` | Half of a badge's height. |