Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a top bar to a bottom navigation view item

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.enter image description here

I dont have any idea how to do it

like image 287
Amanda Pinto Avatar asked Jul 23 '19 09:07

Amanda Pinto


People also ask

How do I add navigation to the bottom view?

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.

What is the bar at the bottom of android called?

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.


1 Answers

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)
like image 168
Farouq Afghani Avatar answered Sep 28 '22 19:09

Farouq Afghani