material-components-android
material-components-android copied to clipboard
[BadgeDrawable] BadgeDrawable appears clipped when used with a Toolbar
Description:
I've been trying to use a BadgeDrawable
on a MenuItem
in our Toolbar
, but it can't really get it to work as expected. It appears as if the value gets visually clipped due to exceeding the menu item view bounds, and I think it's due to the positioning of the badge being too far right.
number = 99, too far to the right:
number = 300, actual visible clipping going on here:
Expected behavior:
Source code: https://github.com/karllindmark/MaterialBadgeDrawableInToolbar (more precisely in MainActivity.kt)
Android API version: Tested on 23, 29 and 30 - all of them display the badge incorrectly
Material Library version: 1.4.0-rc01
Device: Emulator
To help us triage faster, please check to make sure you are using the latest version of the library.
We also happily accept pull requests.
The problem is visible in the catalog
app if you hide the following two menu items in cat_topappbar_menu.xml
:
That renders the menu item as follows (after 100 taps):
what's new about this?
Our designers are having a redesign of badges. Will raise this to them during the redesign.
Definitely a good design issue to consider. In the meantime, you could set BadgeDrawable.setBadgeGravity to a different location if you need a workaround.
Was this forgotten? Unfortunately still happens for me.
Hi, it's not forgotten.
We have the new design and plan to work on this in the next Material version (1.9).
Unfortunately the new design will still have this issue; there's no way around this issue without increasing padding on the menu items or moving the badge gravity to not be at 'END'. As Hunter mentioned, a workaround would be moving the badge to a different location using the gravity, or simply using a small badge.
The guidelines do say "Use a small badge when spaces are tightly constrained, such as top app bars." https://m3.material.io/components/badges/guidelines#92c9d4f0-e787-48ad-a9ad-d1830d4bffc1