Yazan Allahham Yazan Allahham - 1 year ago 132
Android Question

How to change the order of MenuItems in menu of NavigationView in code?

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">
android:title="Home" />
android:title="Register" />
android:title="Login" />
android:title="Language" />
android:title="Contact Us" />
android:title="Likes" />
android:title="Subscription" />
android:title="Logout" />

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

Answer Source

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

  1. Get a reference to the menu first


  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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download