Display badge on top of bottom navigation bar's icon
Adding badges is natively supported now, using the latest material dependency
add this to your build.gradle
implementation 'com.google.android.material:material:1.1.0-alpha09'
in your layout add this
<!-- The rest of your layout here ....-->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:menu="@menu/bottom_nav_menu"
/>
then you can just
val navBar = findViewById<BottomNavigationView>(R.id.bottom_navigation)
navBar.getOrCreateBadge(R.id.action_add).number = 2
R.id.action_add for you would be the id of the menu item you want to put a badge on. Check it from the menu file you feed to the BottomNavigationView.
Make sure your app theme is in Theme.MaterialComponents
you can check it in styles or manifest.
for this example mine was this
<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>
<item name="android:statusBarColor" tools:targetApi="lollipop">@color/colorPrimary</item>
</style>
How to display badge on items of bottomNavigationView android?
You should change your code to this
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(1);
Edit
To have more items having badge, you need to create another layout and so on
BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(0);
notificationBadge = LayoutInflater.from(this).inflate(R.layout.notification_badge, menuView, false);
TextView textView = notificationBadge.findViewById(R.id.counter_badge);
textView.setText("15");
itemView.addView(notificationBadge);
BottomNavigationMenuView menuView1 = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
BottomNavigationItemView itemView1 = (BottomNavigationItemView) menuView1.getChildAt(1);
notificationBadgeOne = LayoutInflater.from(this).inflate(R.layout.notification_badge_one, menuView1, false);
TextView textView = notificationBadgeOne.findViewById(R.id.counter_badge);
textView.setText("15");
itemView1.addView(notificationBadgeOne);
Android BottomNavigationView with Badge
---Note---
Badges will soon be supported out the box. However this might be useful if you really want to add custom views to your tabs.
Create Layout With BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/tabsRootFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tabsRootBottomNavigation" />
<android.support.design.widget.BottomNavigationView
android:id="@+id/tabsRootBottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
local:menu="@menu/root_bottom_navigation_menu"
local:itemIconTint="@color/bottom_navigation_selector"
local:itemTextColor="@color/bottom_navigation_selector"
local:elevation="16dp" />
</RelativeLayout>
Menu: root_bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/tab_search"
android:enabled="true"
android:icon="@drawable/search_icon"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tab_profile"
android:enabled="true"
android:icon="@drawable/profile_icon"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tab_my_car"
android:enabled="true"
android:icon="@drawable/car_icon"
app:showAsAction="ifRoom" />
<item
android:id="@+id/tab_notifications"
android:enabled="true"
android:icon="@drawable/bell_icon"
app:showAsAction="ifRoom" />
</menu>
Create Badge Layout: component_tabbar_badge.axml
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/margin_tiny">
<TextView
android:id="@+id/notificationsBadgeTextView"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="top|center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="@drawable/notification_red_dot"
android:gravity="center"
android:textColor="@color/white"
android:textSize="9dp" />
</FrameLayout>
Red Dot Background: notification_red_dot.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/red" />
<size
android:width="10dp"
android:height="10dp" />
</shape>
Add Badge Layout To Bottom Navigation
private void SetNotificationBadge()
{
_bottomNavigation = FindViewById<BottomNavigationView>(Resource.Id.tabsRootBottomNavigation);
var notificationsTab = _bottomNavigation.FindViewById<BottomNavigationItemView>(Resource.Id.tab_notifications);
View badge = LayoutInflater.From(this).Inflate(Resource.Layout.component_tabbar_badge, notificationsTab, false);
_notificationBadgeTextView = badge.FindViewById<TextView>(Resource.Id.notificationsBadgeTextView);
notificationsTab.AddView(badge);
}
Bind Badge Text
var set = this.CreateBindingSet<TabsRootActivity, TabsRootViewModel>();
set.Bind(_notificationBadgeTextView).To(vm => vm.UnreadNotificationsCount);
set.Apply();
Result
Displaying notification badge on BottomNavigationBar's Icon
Yes. It can be done by stacking two icons using the Stack
and Positioned
widget.
new BottomNavigationBarItem(
title: new Text('Home'),
icon: new Stack(
children: <Widget>[
new Icon(Icons.home),
new Positioned( // draw a red marble
top: 0.0,
right: 0.0,
child: new Icon(Icons.brightness_1, size: 8.0,
color: Colors.redAccent),
)
]
),
)
How to add badge to BottomNavigationBarItem in Flutter?
Remove the const
keyword before declaring the items inside BottomNavigationBar
How to display badge for a menuItem of BottomNavigationView of material library (version 1.1.0-alpha08)?
- 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:
val menuItemId: Int = btm_nav.menu.getItem(0).itemId //0 menu item index.
badgeDrawable = btm_nav.getOrCreateBadge(menuItemId)
badgeDrawable.isVisible = true
badgeDrawable.number = 10
badgeDrawable.badgeGravity = BadgeDrawable.TOP_END //badge gravity
//BadgeDrawable.TOP_START
//BadgeDrawable.BOTTOM_END
//BadgeDrawable.BOTTOM_START
badgeDrawable.isVisible = false //hide badge
badgeDrawable.clearNumber()
XML layout:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/btm_nav"
style="@style/Widget.Design.BottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav_menu"/>
Related Topics
Android Take Screen Shot Programmatically
How to Transfer an Image from Its Url to the Sd Card
How to Access Getsupportfragmentmanager() in a Fragment
How to Get My Android Device Country Code Without Using Gps
When to Register/Unregister Broadcast Receivers Created in an Activity
Android: How to Select Texts from Webview
Location Needs to Be Enabled for Bluetooth Low Energy Scanning on Android 6.0
How to Convert Ppi into Dpi for Android Images
Android Camera Surfaceview Orientation
Sliding Drawer Appear in All Activities
How to Make My Android App Appear in the Share List of Another Specific App