This library offers a simple method to add a small badge icon to your ActionBar-MenuItem

Overview

Android-ActionItemBadge Maven Central Android Arsenal

Join the chat at https://gitter.im/mikepenz/Android-ActionItemBadge

ActionItemBadge is a library which offers a simple and easy to use method to add a badge to your action item!

Screenshots

Image Image

Include in your project

Using Maven

The ActionItemBadge Library is pushed to [Maven Central], so you just need to add the following dependency to your build.gradle.

dependencies {
	implementation 'com.mikepenz:actionitembadge:4.0.0'

	//SUB-DEPENDENCIES
	//Android-Iconics - used to provide an easy API for icons 
	implementation 'com.mikepenz:iconics-core:{latestVersion}@aar'
}

Additional dependency for the icon font

If you are going to use the icon font you will have to add additional dependency for the font. You can find all available addons here: https://github.com/mikepenz/Android-Iconics#2-choose-your-desired-fonts

UPGRADE NOTES

< 4.0.0

  • If you come from a version prior 4.0.0 you will have to upgrade to AndroidX and Iconics v4

< 3.0.0

  • If you come from a version prior 3.0.0 you will have to rename some classes, and the default styles also found a new place. Just check out the updated sample app for all the changes.

Usage

menu.xml

Create your menu.xml as you would do normally and add the app:actionLayout param. It is also a good idea to set showAsAction="always" (The badge can only be shown in the actionbar)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_samplebadge"
        app:actionLayout="@layout/menu_action_item_badge"
        app:showAsAction="always"
        android:title="@string/sample_1"/>
</menu>

Activity

Override the onCreateOptionsMenu method

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

	    //you can add some logic (hide it if the count == 0)
        if (badgeCount > 0) {
            ActionItemBadge.update(this, menu.findItem(R.id.item_samplebadge), FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyles.DARK_GREY, badgeCount);
        } else {
            ActionItemBadge.hide(menu.findItem(R.id.item_samplebadge));
        }

	    //If you want to add your ActionItem programmatically you can do this too. You do the following:
        new ActionItemBadgeAdder().act(this).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).add(bigStyle, 1);
        return super.onCreateOptionsMenu(menu);
    }

If you want to update the item itself you can do the required stuff in the onOptionsItemSelected method and call invalidateOptionsMenu() afterwards.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.item_samplebadge) {
            Toast.makeText(this, R.string.sample_3, Toast.LENGTH_SHORT).show();
            badgeCount--;
            ActionItemBadge.update(item, badgeCount);
            return true;
        } else if (id == SAMPLE2_ID) {
            Toast.makeText(this, R.string.sample_4, Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

Dependencies

Developed By

License

Copyright 2019 Mike Penz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • onOptionsItemSelected not called on Fragment

    onOptionsItemSelected not called on Fragment

    The library binds the onclick event to Activity.onOptionsItemSelected. This results in only the Activity onOptionsItemSelected is called, the Fragments are not.

    This event should be dispatched to the Activity.onMenuItemSelected method which properly calls onOptionsItemSelected on both the Activity and its Fragments.

    enhancement 
    opened by WonderCsabo 12
  • Menu Icon not displaying.

    Menu Icon not displaying.

    Here is my menu_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">
    
        <item
            android:id="@+id/action_notification"
            android:orderInCategory="300"
            android:title="User"
            app:actionLayout="@layout/menu_action_item_badge"
            android:icon="@drawable/ic_notifications_black_24dp"
            app:showAsAction="always">
        </item>
    
    </menu>
    

    Inside MainActivity.java

    MenuItem item= menu.findItem(R.id.action_notification);
                ActionItemBadge.update(item,1);
    

    I am calling this inside this segment:

    private BroadcastReceiver fcmReciever = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                //Badger badge= Badger.getInstance();
    
                Toast.makeText(context,menu.findItem(R.id.action_notification).getTitle().toString(),Toast.LENGTH_LONG).show();
                //menu.findItem(R.id.action_notification).setIcon(R.drawable.camera);
    
                MenuItem item= menu.findItem(R.id.action_notification);
                ActionItemBadge.update(item,1);
                //MainActivity.this.onCreateOptionsMenu(menu);
                //new ActionItemBadgeAdder().act(((Activity) context)).menu(menu).
                //Toast.makeText(context,"recieved",Toast.LENGTH_LONG).show();
            }
        };
    

    Here is the screenshot: error

    question 
    opened by SachinBahukhandi 11
  • Same questions

    Same questions

    Good day. Thx for library. I have 3 question.

    Why I can't use this library without com.mikepenz:iconics?

    Can you create BadgeStyle with circle?

    Method 'hide' hide my item too. How can I hide only badge?

    question 
    opened by AlexeyKorshun 9
  • Remove badge on Run time using fragment

    Remove badge on Run time using fragment

    Hi,
    Thanks for the awesome library. How can I remove only badge on runtime but not hiding entire menu item in fragment.

    Thanking you in advance for your time

    question 
    opened by vinayaksrs 8
  • Is there a way to simply show the un-badged icon?

    Is there a way to simply show the un-badged icon?

    It seems from readme, and when I tried out that I can just update to a given badge or hide the icon in menu completely - is there a way to show the normal icon without a badge?

    thanks

    help wanted 
    opened by somghosh 7
  • Cant populate items from firebase to show listView count to the Badge.

    Cant populate items from firebase to show listView count to the Badge.

    The badge doesnt populate when receiving the ListView item from firebase console. It works only when it come in the foreground, It doesnt update when we fire a new ListView item or the app is in Background or Killed?

    opened by Jaypatelbond 6
  • Library not working with Maven  - proposed solution

    Library not working with Maven - proposed solution

    Hello,

    seems like your pom.xml is missing something to make this library work with maven-android-plugin

    specifically, you should change your dependency inside your pom.xml to be:

     <dependency>
          <groupId>com.mikepenz.iconics</groupId>
          <artifactId>library</artifactId>
          <type>aar</type>
          <version>0.7.0</version>
          <scope>compile</scope>
        </dependency>
    

    (i just added the "type" part) Thanks

    help wanted 
    opened by athkalia 5
  • Can't find  class file for com.mikpenz.iconics.typeface.IIcon not found

    Can't find class file for com.mikpenz.iconics.typeface.IIcon not found

    I used the latest Android studio to try it, but get the following error,

    added 2 dependencies

    compile 'com.joanzapata.android:android-iconify:1.0.8' compile 'com.mikepenz.actionitembadge:library:2.0.0@aar'

    Error:(48, 28) error: cannot access IIcon class file for com.mikpenz.iconics.typeface.IIcon not found

    bug 
    opened by stonyz 5
  • Appbar Icon Disappears After Update

    Appbar Icon Disappears After Update

    Hello my Appbar Icon Disappears after i update it!

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.app_bar_main, menu);
    
            int badgeCount =5;
    
            if (badgeCount > 0) {
                ActionItemBadge.update(this, menu.findItem(R.id.notify_me), (Drawable) Iconics.findFont(this,"Fontawesome"), ActionItemBadge.BadgeStyles.RED, badgeCount);
            } else {
                ActionItemBadge.hide(menu.findItem(R.id.notify_me));
            }
            return super.onCreateOptionsMenu(menu);
        }
    
    question 
    opened by frankodoom 4
  • ClassCastException

    ClassCastException

    Version 3.2.3, Android 6 (Genymotion)

    java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.GradientDrawable
                                                                           at com.mikepenz.actionitembadge.library.utils.BadgeDrawableBuilder.build(BadgeDrawableBuilder.java:39)
    
    bug 
    opened by bulatgaleev 4
  • getting null pointer exception

    getting null pointer exception

    in my menu xml i placed this..

    <item android:id="@+id/action_carticon" android:title="@string/action_example"
            app:showAsAction="always"
            android:orderInCategory="100"
            android:actionLayout="@layout/menu_badge"
            />
    

    and in my create options menu i used this

    ActionItemBadge.update(this, menu.findItem(R.id.action_carticon), getResources().getDrawable(R.drawable.ic_launcher), ActionItemBadge.BadgeStyle.GREEN,5);
    

    what i done wrong if i create menu item programatically works great.if the count is zero need to hide the item badge.is there any possible.

    bug 
    opened by Aviswanathan 4
Releases(v4.0.0)
An Android library supports badge notification like iOS in Samsung, LG, Sony and HTC launchers.

ShortcutBadger: The ShortcutBadger makes your Android App show the count of unread messages as a badge on your App shortcut! Supported launchers: Sony

Leo Lin 7.2k Dec 30, 2022
:octocat: Drawable of badge.

Badge Preview Integration Add the JitPack repository to your root build.gradle: repositories { maven { url "https://jitpack.io" } } Add the depend

nekocode 952 Dec 8, 2022
SmileyRating is a simple rating bar for android. It displays animated smileys as rating icon.

Smiley Rating SmileyRating is a simple rating bar for android. It displays animated smileys as rating icon. Drawn completely using android canvas Insp

Sujith Niraikulathan 1.1k Dec 22, 2022
This library provides a simple way to add a draggable sliding up panel (popularized by Google Music and Google Maps) to your Android application. Brought to you by Umano.

Note: we are not actively responding to issues right now. If you find a bug, please submit a PR. Android Sliding Up Panel This library provides a simp

Umano: News Read To You 9.4k Dec 31, 2022
A simple library to add Emoji support to your Android Application

Emoji A library to add Emoji support to your Android app. Emojis can be picked in a PopupWindow. In order to edit and display text with Emojis this li

Niklas Baudy 1.4k Jan 4, 2023
Android integration of multiple icon providers such as FontAwesome, Entypo, Typicons,...

Note: Due to lack of time, Iconify is no longer maintained and icon packs are outdated. I'd be very happy to welcome a new contributor, please reach m

Joan Zapata 3.9k Dec 24, 2022
A material Switch with icon animations and color transitions

Material Animated Switch A material Switch with icon animations and color transitions Sample video: Youtube Material Animated Switch video Sample app:

Adrián Lomas 1.2k Dec 29, 2022
MIUINativeNotifyIcon - Fix the native notification bar icon function abandoned by the MIUI development team

MIUI 原生通知图标 Fix the native notification bar icon function abandoned by the MIUI

Fankesyooni 189 Jan 4, 2023
Add some depth to your Android scrolling.

Parallax Pager Add some depth to your Android scrolling using the parallax effect! Installation Step 1. Add the JitPack repository to your build file

Prolific Interactive 782 Dec 29, 2022
A small, easy to use android library for implementing flipping between views as seen in the popular Flipboard application

FlipView About This library is made to be very easy to use and at the same time be feature complete. With only a few lines of code you can have a flip

Emil Sjölander 924 Nov 10, 2022
TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View

TourGuide TourGuide is an Android library. It lets you add pointer, overlay and tooltip easily, guiding users on how to use your app. Refer to the exa

Tan Jun Rong 2.6k Jan 5, 2023
Android view that allows the user to create drawings. Customize settings like color, width or tools. Undo or redo actions. Zoom into DrawView and add a background.

DrawView Android view that allows the user to create drawings. Draw anything you like in your Android device from simple view. Customize draw settings

Oscar Gilberto Medina Cruz 839 Dec 28, 2022
PCard Add payment card screen made using JetPack Compose

PCard Add payment card screen made using JetPack Compose Find this repository useful? ❤️ Support it by joining stargazers for this repository. ⭐ And f

Mohamed Elbehiry 61 Dec 16, 2022
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.

Bubbles for Android Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your

Txus Ballesteros 1.5k Jan 2, 2023
Make your native android Toasts Fancy. A library that takes the standard Android toast to the next level with a variety of styling options. Style your toast from code.

FancyToast-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ... ma

Shashank Singhal 1.2k Dec 26, 2022
Make your native android Dialog Fancy. A library that takes the standard Android Dialog to the next level with a variety of styling options. Style your dialog from code.

FancyAlertDialog-Android Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { repositories { ..

Shashank Singhal 350 Dec 9, 2022
A simple library to let you sign (or draw lines) smoothly with your finger into a view and save it.

FingerSignView Introduction FingerSignView is a simple library that lets you finger, or draw lines, smoothly with your finger into a View and save it

Agnaldo Pereira 25 Nov 20, 2022
Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Rizki Maulana 118 Dec 14, 2022
A simple screen that is shown when your app gets crashed instead of the normal crash dialog. It's very similar to the one in Flutter.

Red Screen Of Death What A simple screen that is shown when your app gets crashed instead of the normal crash dialog. It's very similar to the one in

Ahmad Melegy 178 Dec 9, 2022