Skip to content

Instantly share code, notes, and snippets.

@rajsuvariya
Last active September 18, 2017 05:08
Show Gist options
  • Save rajsuvariya/64cbc6501c1d14afc3d9ebc92167da0f to your computer and use it in GitHub Desktop.
Save rajsuvariya/64cbc6501c1d14afc3d9ebc92167da0f to your computer and use it in GitHub Desktop.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
setCount(this, "9");
}
public void setCount(Context context, String count) {
MenuItem menuItem = defaultMenu.findItem(R.id.ic_group);
LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
CountDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_group_count);
if (reuse != null && reuse instanceof CountDrawable) {
badge = (CountDrawable) reuse;
} else {
badge = new CountDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_group_count, badge);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment