BadgeUtils

@ExperimentalBadgeUtils
public class BadgeUtils


Utility class for BadgeDrawable.

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

Summary

Public methods

static void
attachBadgeDrawable(BadgeDrawable badgeDrawable, View anchor)
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,
    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,
    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>
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, 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 or its anchor's FrameLayout ancestor if it has a custom parent set.

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.

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 SavedStates, creates a parcelable map of int keys to BadgeDrawbles. Useful for state restoration.

Parameters
Context context

Current context

ParcelableSparseArray badgeStates

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
SparseArray<BadgeDrawable> badgeDrawables

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