material-components / material-components/material-components-android
[NavigationView] Update visibility not working for programmatically added menu item.
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
**Description:** When we add a menu item programmatically and then change its visibility, the menu item is still visible.
`binding.navView.menu.add(R.id.otherMenuGroup, 999, 300, null).setActionView(R.layout.item_menu)`
```
private fun updateMenu() {
val menuItemCount: Int = binding.navView.menu.size()
for (i in 0 until menuItemCount) {
val menuItem: MenuItem = binding.navView.menu.getItem(i)
val visible = isParentGroupVisible(menuItem)
menuItem.isVisible = visible
menuItem.isEnabled = visible
}
binding.navView.invalidate()
}
```
private fun isParentGroupVisible(menuItem: MenuItem): Boolean {
val groupId = menuItem.groupId
if (groupId == R.id.mainMenuGroup) {
return isMainMenu
} else if (groupId == R.id.otherMenuGroup) {
return !isMainMenu
}
return false
}
**Actual behavior:**
As you can see _**Items with error**_ menu item is still visible on scenario 3
|  |  |  |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| User open app | The user clicks on the Android image, and the update menu item is called. | The user clicks on the Android image again, and the update menu item is called. |
**Expected behavior:**
_**Items with error**_ menu item should not be visible on scenario 3
|  |  |  |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| User open app | The user clicks on the Android image, and the update menu item is called. | The user clicks on the Android image again, and the update menu item is called. |
**Source code:**
[TestDrawer.zip](https://github.com/user-attachments/files/18656955/TestDrawer.zip)
**Material Library version:**
Version 1.13.0-alpha10.
**Note:** works correctly on 1.11.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the visibility update with the attached TestDrawer.zip and compare behavior between Material Library 1.13.0-alpha10 and 1.11.0. Trace the programmatically added NavigationView menu item through the updateMenu call and verify that the item is hidden in scenario 3 without regressing the earlier scenarios.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100