5

I have a menu in NavigationView that have 7 menu items, some of these menu items should be invisible based on user settings and the remaining visible items should be displayed in different order. the items are already defined in the XML menu layout.

I've googled a lot but nothing related to already defined menu items. most of the solutions was suggesting to create the menu items in code and set the order while creating it.

here is my menu items XML layout:

<group android:checkableBehavior="single">
    <item
        android:id="@+id/Home_menuitem"
        android:title="Home" />
    <item
        android:id="@+id/Register_menuitem"
        android:title="Register" />
    <item
        android:id="@+id/Login_menuitem"
        android:title="Login" />
    <item
        android:id="@+id/language_menuitem"
        android:title="Language" />
    <item
        android:id="@+id/ContactUs_menuitem"
        android:title="Contact Us" />
    <item
        android:id="@+id/Likes_menuitem"
        android:title="Likes" />
    <item
        android:id="@+id/Subscription_menuitem"
        android:title="Subscription" />
    <item
        android:id="@+id/Logout_menuitem"
        android:title="Logout" />
</group>

say I want to change the order in Code (NOT in XML) to make "Likes_menuitem" to be displayed above "Home_menuitem"

Yazan Allahham
  • 174
  • 2
  • 17

3 Answers3

4

I didn't try it but you probably can follow these steps.

  1. Get a reference to the menu first

    navigationView.getMenu()

  2. Add or remove specific items to (from) it with different order to achieve your order.

    add(int groupId, int itemId, int order, CharSequence title)

    removeItem(int id)

More information in the docs!

cylon
  • 735
  • 1
  • 11
  • 26
  • As you can see the Add method will create new menu item! it doesn't use the MenuItem you already have it in the XML...I'm looking for a simple way to do it just like add(MenuItem, Order) – Yazan Allahham Sep 15 '16 at 18:02
  • You can get the existing item with `findItem(int id)` but the returned `MenuItem` has no method like `setOrder(int order)`. So maybe this is the only way to do this. Isn't complicated, did you try it at least? – cylon Sep 15 '16 at 18:15
  • I know it is not complicated but i was looking for a proper way to do it! anyway I will check it right now and get back with feedback. – Yazan Allahham Sep 15 '16 at 18:18
  • For some reason the order parameter in `add` method doesn't work for me. It always places the added menu item at the last position. – YoussefDir Apr 20 '20 at 14:24
  • 1
    Well, I solved it. Make sure you use `android:orderInCategory` for all items if you already had before (in your xml menu file)... See the comment of Joe Muller here https://stackoverflow.com/questions/28715765/what-is-orderincategory-in-actionbar-menu-item-why-it-is-use-for – YoussefDir Apr 22 '20 at 22:41
0

If you add items programmatically just change the order of adding items. Even if you change the position of menu inflating it will affect the result order.

menu.add(Menu.NONE, 898, Menu.NONE, "Item title") // <- Will be 1st
inflater.inflate(R.menu.menu_progress_avatar, menu) // <- Will be 2nd
Daniel
  • 2,415
  • 3
  • 24
  • 34
-1

You can do this by visible menuitem to true or false.

D.J
  • 1,439
  • 1
  • 12
  • 23
  • 1
    You cannot change the order by changing the visibility of the item...the question is very clear! it is about changing the order of the item in the menu – Yazan Allahham Sep 16 '16 at 12:45