5
<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.

Arpana K.C.
  • 51
  • 1
  • 3
  • 1
    Possible duplicate of [Display badge on top of bottom navigation bar's icon](https://stackoverflow.com/questions/42682855/display-badge-on-top-of-bottom-navigation-bars-icon) – skylerb Jul 03 '18 at 17:28
  • https://stackoverflow.com/a/57073610/7254873 – Sumit Shukla Jul 26 '19 at 05:42

3 Answers3

4

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">

Atif Amin
  • 490
  • 2
  • 6
  • 17
1

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);`
Manish Arora
  • 397
  • 3
  • 12
-1

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