Search code examples
androidmaterial-designbottomnavigationviewmaterial-componentsmaterial-components-android

How to change the color of BottomNavigationView badge background?


I have implemented BottomNavigationView with badge count based on this example https://stackoverflow.com/a/56340643/6699103

it works fine but I wanted to change the background color for badge Is there any way to do that?


Solution

  • Just use the method setBackgroundColor() of the badge.

      BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
      badge.setNumber(..);
      badge.setBackgroundColor(....);
    

    enter image description here

    If you want to change globally in your app the style you can define the badgeStyle attr in your app theme:

    <style name="AppTheme" parent="Theme.MaterialComponents.*">
      <item name="badgeStyle">@style/CustomBadge</item>
      ...
    </style>
    
    <style name="CustomBadge" parent="@style/Widget.MaterialComponents.Badge">
       <item name="backgroundColor">@color/.....</item>
    </style>