BadgeUtils

public class BadgeUtils
extends Object

java.lang.Object
   ↳ com.google.android.material.badge.BadgeUtils


Utility class for BadgeDrawable.

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

Summary

Fields

public static final boolean USE_COMPAT_PARENT

Public methods

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.

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.

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.

static void attachBadgeDrawable(BadgeDrawable badgeDrawable, View anchor)
static SparseArray<BadgeDrawable> createBadgeDrawablesFromSavedStates(Context context, ParcelableSparseArray badgeStates)

Given a map of int keys to SavedStates, creates a parcelable map of int keys to BadgeDrawbles.

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.

static void detachBadgeDrawable(BadgeDrawable badgeDrawable, View anchor)

Detaches a BadgeDrawable from its associated anchor.

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

Detaches a BadgeDrawable from its associated action menu item on a toolbar, For API 18+, the BadgeDrawable will be removed from its anchor's ViewOverlay.

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

Sets the bounds of a BadgeDrawable to match the bounds of its anchor (for API 18+) or its anchor's FrameLayout ancestor (pre-API 18).

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.

Inherited methods

Fields

USE_COMPAT_PARENT

public static final boolean USE_COMPAT_PARENT

Public methods

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. For API 18+, the BadgeDrawable will be added as a view overlay. For pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an ancestor of the anchor.

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.

Parameters
badgeDrawable BadgeDrawable

toolbar Toolbar

menuItemId int

customBadgeParent FrameLayout

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.

Parameters
badgeDrawable BadgeDrawable

toolbar Toolbar

menuItemId int

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. For API 18+, the BadgeDrawable will be added as a view overlay. For pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an ancestor of the anchor.

Parameters
badgeDrawable BadgeDrawable

anchor View

customBadgeParent FrameLayout

attachBadgeDrawable

public static void attachBadgeDrawable (BadgeDrawable badgeDrawable, 
                View anchor)

Parameters
badgeDrawable BadgeDrawable

anchor View

createBadgeDrawablesFromSavedStates

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

Given a map of int keys to SavedStates, creates a parcelable map of int keys to BadgeDrawbles. Useful for state restoration.

Parameters
context Context: Current context

badgeStates ParcelableSparseArray: A parcelable SparseArray that contains a map of int keys (e.g. menuItemId) to states.

Returns
SparseArray<BadgeDrawable> A SparseArray that contains a map of int keys (e.g. menuItemId) to BadgeDrawables.

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
badgeDrawables SparseArray: A SparseArray that contains a map of int keys (e.g. menuItemId) to BadgeDrawable BadgeDrawables.

Returns
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. For API 18+, the BadgeDrawable will be removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from the foreground of a FrameLayout that is an ancestor of the anchor.

Parameters
badgeDrawable BadgeDrawable

anchor View

detachBadgeDrawable

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

Detaches a BadgeDrawable from its associated action menu item on a toolbar, For API 18+, the BadgeDrawable will be removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from the foreground of a FrameLayout that is an ancestor of the anchor.

Parameters
badgeDrawable BadgeDrawable

toolbar Toolbar

menuItemId int

setBadgeDrawableBounds

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

Sets the bounds of a BadgeDrawable to match the bounds of its anchor (for API 18+) or its anchor's FrameLayout ancestor (pre-API 18).

Parameters
badgeDrawable BadgeDrawable

anchor View

compatBadgeParent FrameLayout

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.

centerX float: A badge's center x coordinate.

centerY float: A badge's center y coordinate.

halfWidth float: Half of a badge's width.

halfHeight float: Half of a badge's height.