I want to put a bar in the top of each Bottom Navigation View Item's items when it is selected. As the image below, but i don't find the way to do it.
I dont have any idea how to do it
Right-click on the res folder and select Android Resource Directory. Make sure to select the resource type as a menu. Now create the bottom_menu.
The Navigation bar is the menu that appears on the bottom of your screen - it's the foundation of navigating your phone. However, it isn't set in stone; you can customize the layout and button order, or even make it disappear entirely and use gestures to navigate your phone instead.
You can achieve it by adding a view on the bottom navigation, check the code below, you can also use this to add any view on the bottom navigation item, such as Badge, small icon, etc..
Layout xml for the bar
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="2dp"
android:layout_gravity="center_horizontal"
android:background="@color/red"
android:gravity="center"/>
Controller (show/hide)
class BottomNavigationHelper {
fun showBadge(context: Context, bottomNavigationView: BottomNavigationView, @IdRes itemId: Int) {
removeBadge(bottomNavigationView, itemId)
val itemView = bottomNavigationView.findViewById<BottomNavigationItemView>(itemId)
val badge = LayoutInflater.from(context).inflate(R.layout.layout_red_badge, bottomNavigationView, false)
itemView.addView(badge)
}
fun removeBadge(bottomNavigationView: BottomNavigationView, @IdRes itemId: Int) {
val itemView = bottomNavigationView.findViewById<BottomNavigationItemView>(itemId)
if (itemView.childCount == 3) {
itemView.removeViewAt(2)
}
}
}
sample call
BottomNavigationHelper().showBadge(mContext, bottomNavigationView, R.id.navigation_home)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With