Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable menu item of bottom navigation view?

How can I disable a material design bottom navigation menu item? I can set .isClickable = false but this doesn't show the menu item as disabled, similar to a button. I can't do .isEnabled, the API won't allow it.

BottomNavigationView XML

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/menu_item_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:background="@color/colorDark"
    app:menu="@menu/bottom_navigation_menu">

Menu XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
      
    <item
        android:id="@+id/action_home"
        android:title="Home"
        android:icon="@drawable/home_button"
        app:showAsAction="ifRoom" />

Activity

class Something : AppCompatActivity() {
    private lateinit var mHomeBtn: BottomNavigationItemView
    override fun onCreate(...) {
        mHomeBtn = this.findViewById(R.id.action_home)
        mHomeBtn.isClickable = false  // <--- will make it unable to click but won't show disabled
        mHomeBtn.isEnabled = false    // <--- will throw an error
        mHomeBtn.setOnClickListener(this)
    }
    ...
like image 265
EvOlaNdLuPiZ Avatar asked Jan 01 '23 09:01

EvOlaNdLuPiZ


2 Answers

You should get the menu of your BottomNavigationView and then find and disable the MenuItem that you want. In code it could be done as follows

override fun onCreate(savedInstanceState: Bundle?) {
    // Find the bottom navigation view, (Use correct ID)
    // menu_item_1 is probably not a good ID for a navigation view
    val navView: BottomNavigationView = findViewById(R.id.nav_view)

    // Find the menu item and then disable it
    navView.menu.findItem(R.id.navigation_home).isEnabled = false
}
like image 166
mightyWOZ Avatar answered Jan 04 '23 16:01

mightyWOZ


Adding to mightyWOZ answer

navView.menu.findItem(R.id.navigation_home).isCheckable = false

this makes the menu item disabled (grey).

like image 39
Potato Avatar answered Jan 04 '23 16:01

Potato