Search code examples
androidandroid-layoutmenumenuitemmenu-items

Removing an android menu item from code


I'm trying to make a subscription list in a navigation view where when you subscribe to an item the category of that item will show in the navigation view, and when you unsubscribe from every item in a category it should remove that category from the navigation view's menu.

Problem is to remove the category I need to clear the whole list and then add item by item, but the navigation view doesn't hold just the list of subscription: It also allows the user to navigate through different activities. If I use the menu.clear() method the navigation items will be removed as well.

To add them again, I need as parameters the item's ID, the group's ID, the Order int and the title. I know where to get the IDs and titles since they are coded in the XML, but I know not how to find the order in the list, or what that is.

Also if you have an easier or better way to resolve this, please let me know.


Solution

  • I was told the RemoveItem could cause problems, so I tried to avoid that.

    What I did was find the item and group ID from the items I didnt want to get removed, clear the list, and then add the items with a made up order (I just made a variable and incremented it for each item I needed), then add the item names I needed for the items without interaction.