Search code examples
androidbottombar

How to add a number to Bottom Bar like the image


screen shot

I am currently using a smooth bottom bar library.


Solution

  • you want to add badge count in BottomNavigationView menu?

    then use MaterialComponents library

    implementation 'com.google.android.material:material:1.2.0-alpha03'
    

    use materialcomponent theme style.xml

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
    </style>
    

    in your layout file

    <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:menu="@menu/bottom_nav_menu"/>
    

    add badge count like

    val bottonnavBar = findViewById<BottomNavigationView>(R.id.bottom_nav)
    var badge = bottonnavBar.getOrCreateBadge(R.id.action_add) //R.id.action_add is menu id
    badge.number = 2 // it will show count on icon
    

    hope it helpss...