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)
}
...
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
}
Adding to mightyWOZ answer
navView.menu.findItem(R.id.navigation_home).isCheckable = false
this makes the menu item disabled (grey).
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