Is there a android standard badge or method to show action bar notification icon with a count like on Google examples?
If not, then what is the best way to make it?
I'm new to android, please help.
Is there a android standard badge or method to show action bar notification icon with a count like on Google examples?
If not, then what is the best way to make it?
I'm new to android, please help.
I am not sure if this is the best solution or not, but it is what I need.
Please tell me if you know what is need to be changed for better performance or quality. In my case, I have a button.
Custom item on my menu - main.xml
Custom shape drawable (background square) - shape_notification.xml
Layout for my view - feed_update_count.xml
MainActivity - setting and updating my view
Just to add. If someone wants to implement a filled circle bubble, heres the code (name it
bage_circle.xml
):You may have to adjust the thickness according to your need.
EDIT: Here's the layout for button (name it
badge_layout.xml
):In Menu create item:
In
onCreateOptionsMenu
get reference to the Menu item:After receiving notification for messages, set the count:
This code uses Iconify-fontawesome.
I don't like
ActionView
based solutions, my idea is:TextView
, thatTextView
will be populated by applicationwhen you need to draw a
MenuItem
:2.1. inflate layout
2.2. call
measure()
&layout()
(otherwiseview
will be 0px x 0px, it's too small for most use cases)2.3. set the
TextView
's text2.4. make "screenshot" of the view
2.6. set
MenuItem
's icon based on bitmap created on 2.4profit!
so, result should be something like
@drawable/unread_background
is that greenTextView
's background,@drawable/ic_menu_gallery
is not really required here, it's just to preview layout's result in IDE.add code into
onCreateOptionsMenu
/onPrepareOptionsMenu
Implement build-the-icon method:
The complete code is here: https://github.com/cvoronin/ActionBarMenuItemCounter
When you use toolbar:
and in your load data call refreshMenu():
Edit Since version 26 of the support library (or androidx) you no longer need to implement a custom
OnLongClickListener
to display the tooltip. Simply call this:I'll just share my code in case someone wants something like this:
layout/menu/menu_actionbar.xml
layout/action_bar_notifitcation_icon.xml
Note style and android:clickable properties. these make the layout the size of a button and make the background gray when touched.
drawable-xhdpi/ic_bell.png
A 64x64 pixel image with 10 pixel wide paddings from all sides. You are supposed to have 8 pixel wide paddings, but I find most default items being slightly smaller than that. Of course, you'll want to use different sizes for different densities.
drawable/rounded_square.xml
Here, #ff222222 (color #222222 with alpha #ff (fully visible)) is the background color of my Action Bar.
com/ubergeek42/WeechatAndroid/WeechatActivity.java
Here we make it clickable and updatable! I created an abstract listener that provides Toast creation on onLongClick, the code was taken from from the sources of ActionBarSherlock.
Ok, for @AndrewS solution to work with v7 appCompat library:
.
The rest of the code is the same.