How to display badge for a menuItem of BottomNavigationView of material library (version 1.1.0-alpha08)?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:21:42
  • First migrate your project to androidX. How to migrate
  • Include dependency in your build.gradle:

implementation 'com.google.android.material:material:version' Get Version

  • Your Base AppLevel theme should use Material Component Theme like:

Your App Level Theme

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

Activity code:

int menuItemId = btmNavView.getMenu().getItem(0).getItemId();  //0 menu item index.  
BadgeDrawable badgeDrawable = btmNavView.getOrCreateBadge(menuItemId);
badgeDrawable.setVisible(true);  
badgeDrawable.setNumber(1);   //1 represents badge value.

XML layout:

 <com.google.android.material.bottomnavigation.BottomNavigationView
      style="@style/Widget.Design.BottomNavigationView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:menu="@menu/bottom_nav_menu"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!