'How to add notification badge on Bottom Navigation View?

<item
        android:id="@+id/nav_gallery"
        app:actionViewClass="android.widget.TextView"/>

This is the menu of bottom Navigation.

   TextView gallery=(TextView)  
   MenuItemCompat.getActionView(navigationView.getMenu().
   findItem(R.id.nav_gallery)); //getting menu item of bottom nav view
   gallery.setText("99+");

But this code doesnot work for bottom navigation view.BottomNavigationView is shown without setting any notification counter.



Solution 1:[1]

The Latest Material dependency now supports natively add badge count, Need to just updated Material dependency in build.gradle

implementation 'com.google.android.material:material:1.1.0-alpha09'

and just add

val navBar  = findViewById<BottomNavigationView>(R.id.bottom_navigation)
navBar.getOrCreateBadge(R.id.action_stamp).number = 2

and in style.xml just change "AppCompat" to "MaterialComponents"

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">

Solution 2:[2]

Firstly create a layout file of your badge, then follow these steps

BottomNavigationMenuView menuView = (BottomNavigationMenuView) navigation.getChildAt(0);
 
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(2);

View messageBadgeView = LayoutInflater.from(this).inflate(R.layout.message_badge_view, menuView, false);    
TextView textView = messageBadgeView.findViewById(R.id.counter_badge);        
textView.setText("15");

itemView.addView(messageBadgeView);`

Solution 3:[3]

Another way for those who couldnt access the getOrCreaeteBadge is to use the method below;

  1. Make sure your theme on your bottom Navigation is set on: Theme.MaterialComponents.Light.DarkActionBar

  2. Then now:

     String messages = getIntent().getStringExtra("messages_count");
     messages = messages.substring(10,11);
     Log.d("MessagesCount", "getIncomingIntent: "+messages);
     navigation.showBadge(R.id.bottom_messages).setNumber(Integer.parseInt(messages));
    

And you're good to go! Sample output is below

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Manish Arora
Solution 3 Daniel Kabu Asare