1

I'm using a NavigationRailView (com.google.android.material.navigationrail.NavigationRailView) in my Android application and I'm trying to change the colour of a single item in the navigation rail to make it look "disabled". I've tried the following code but while it does disable the menu item, it doesn't actually look disabled:

val menuItem = binding.navRailView.menu.findItem(R.id.item3)
menuItem.isEnabled = false
menuItem.isCheckable = false

Using the follow code I found online doesn't seem to let me change the colour either. (I have verified that the resource colour is correct with another View element)

menuItem.setIconTintList(baseContext.getColorStateList(R.Color.view_disabled))

Anyone has any idea or is it not possible to do so right now?

Pycorax
  • 77
  • 5

1 Answers1

2

Currently it seems that there is no an official way to programmatically change the menu item colour in NavigationRailView but you can change it using a simple workaround like the below example:

//get the NavigationRailView
val navRailView = findViewById<NavigationRailView>(R.id.navigation_rail)

//find the NavigationBarItemView you are interested to using the menu item Id and from there retrieve the item ImageView
val navBarItemView = navRailView.findViewById<NavigationBarItemView>(R.id.item3)
val itemImView = navBarItemView.findViewById<ImageView>(com.google.android.material.R.id.navigation_bar_item_icon_view)
itemImView.setColorFilter(ContextCompat.getColor(this, android.R.color.holo_red_dark))
MariosP
  • 8,300
  • 1
  • 9
  • 30
  • 1
    Thanks, that worked. I'd like to add that I used the following code to change the text color too: `val textImView = navBarItemView.findViewById(com.google.android.material.R.id.navigation_bar_item_small_label_view) textImView.setTextColor(Color.parseColor("#575757"))` – Pycorax Aug 14 '23 at 09:41