9

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?

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Mohan K
  • 464
  • 5
  • 18
  • https://material.io/develop/android/components/bottom-navigation-view/ Follow this site and change style of BottomNavigationView. – Chetan Shelake Oct 22 '19 at 13:02

3 Answers3

31

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>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

This is the solution which I found and it is working as expected

val profileBadge = bottomNavigationView.getOrCreateBadge(R.id.profile)

 profileBadge.number = profileCount

 profileBadge.backgroundColor = ContextCompat.getColor(this@HomeActivity, R.color.green) 
Viral Patel
  • 1,296
  • 1
  • 11
  • 24
Mohan K
  • 464
  • 5
  • 18
0

create drawable selector in res/drawable for colors you wants depending on navigation item state. for example bottom_navigation_colors.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"
        android:color="@color/white" />
    <item
        android:state_checked="false"
        android:color="@color/blue" />
</selector>

and then you can create a style style for your BottomNavigationView

<style name="myBottomNavigationStyle">
    <item name="android:background">#rrggbb</item>
    <item name="itemIconTint">@drawable/bottom_navigation_colors</item>
    <item name="itemTextColor">@drawable/bottom_navigation_colors</item>
</style>

and assign it to BottomNavigationView

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/ebottom_navigation_view"
    style="@style/myBottomNavigationStyle"
    .... />
SebastienRieu
  • 1,443
  • 2
  • 10
  • 20