qwertz
qwertz

Reputation: 6286

Android display notification number next to menu item

I want to display a notification number on a menu item in the overflow stack to indicate there are x updates available. I know you can set an icon to the menu item, but I want to display the number on the right side of the menu item (Instabridge does this to indicate hotspot index is bein updated). How can I solve this?

Upvotes: 2

Views: 2443

Answers (1)

esong
esong

Reputation: 260

I would assume that the overflow stack you're talking about is the OverflowPopup. If not, these two questions may solve your problem: Actionbar notification count icon (badge) like Google has, and Add new item count to icon on button - Android.

For the popup, you may want to create a custom menu view for the notification number, as this pattern is not supported by the library. (There may be better ways of doing that.)

However, creating a custom menu view is a little bit complicated. Here is what I got from reading the source, not tested!

First, You need to create your MenuView.ItemView and add notification number to it. You can either create a new view that implementes this interface or use android's android.support.v7.internal.view.menu.ActionMenuItemView.

Then, You need a customized ActionMenuPresenter to use your menu view. In the ActionMenuPresenter you can change the constructor's second parameter to your view.

public ActionMenuPresenter(Context context) {
    super(context, R.layout.abc_action_menu_layout, R.layout.abc_action_menu_item_layout);
}

or override BaseMenuPresenter.createItemView method.

Finally, you need to wire up the ActionMenuPresenter with your Toolbar.

Please reference the source code for more details.

Upvotes: 1

Related Questions