Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In every application, we decorate our MenuItem placed on ActionBar. Displaying of badge count not only decorate the ActionBar beautifully but also displays useful information like showing the number of items currently available in your cart or update you with unread notification.
We can create badge count using either a custom view or by adding a distinct drawable to display the each state of MenuItem.
Both the options have its pros and cons.
There is another approach using LayerDrawable which is more flexible and efficient than the former approaches.
A Drawable that manages an array of other Drawables. These are drawn in array order, so the element with the largest index will be drawn on top.It can be defined in an XML file with the
It can be defined in an XML file with the <layer-list> element. Each Drawable in the layer is defined in a nested<item>
<layer-list>
<item>
i. Creating a layer drawable to display menu item on layer one and badge on top of it.
ic_menu_cart.xml
ii. Creating menu for our activity to display the count on the menu items.
main.xml
iii. Creating a flexible and efficient custom BagdeDrawable class to draw a view that looks like a count
BagdeDrawable.java
We can get the icon of the menu using getIcon() method.
Here is our method setBadgeCount(). We can pass LayerDrawable which we get from meniItem and setBadge from anywhere from the application.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
setBadgeCount(this, icon,”0″)
This will set mWillDraw to false if the count is “0”. Thus This will not draw the badge or simply remove the badge.
here if I get reuse as null;the onDraw() method of BadgeDrawable is not called at all(so no badge) since the bounds are 0; how do I fix this; sometimes badge will get drawn as sometimes not and it entirely depends on reuse variable (if its null no badge and if its not null, I can see the badge). Thanks, any help will be greatly appreciated 😉
icon.mutate(); icon.setDrawableByLayerId(R.id.ic_badge, badge); badge.setCount(count); //shifted down
Getting the above ClassCastException
MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); MenuItem itemCart = menu.findItem(R.id.action_cart); LayerDrawable icon = (LayerDrawable)itemCart.getIcon(); setBadgeCount(this, icon, “9”); return super.onCreateOptionsMenu(menu);
Thank you Shubham Agarwal.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.