Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add notification badge on Bottom Navigation View?

Tags:

android

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

like image 794
Arpana K.C. Avatar asked Oct 18 '22 13:10

Arpana K.C.


1 Answers

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

like image 184
Atif Amin Avatar answered Oct 21 '22 04:10

Atif Amin