material-components / material-components/material-components-android

[NavigationView] Update visibility not working for programmatically added menu item.

Open
#4,575 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Widget: NavigationView
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

| ![Description 1](https://github.com/user-attachments/assets/6e67035b-066b-4da0-a64b-f4c7c7a5103f) | ![Description 2](https://github.com/user-attachments/assets/d7bfd697-82ab-49ae-baf6-62e487f3d407) | ![Description 3](https://github.com/user-attachments/assets/2dcef2b2-7e57-462e-8138-53b626129ef2) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| 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

| ![Description 1](https://github.com/user-attachments/assets/6e67035b-066b-4da0-a64b-f4c7c7a5103f) | ![Description 2](https://github.com/user-attachments/assets/d7bfd697-82ab-49ae-baf6-62e487f3d407) | ![Description 3](https://github.com/user-attachments/assets/6e67035b-066b-4da0-a64b-f4c7c7a5103f) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|
| 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.