Search code examples
androidandroid-fragmentsoptionmenu

Change option menu position in fragment


I have an activity with an Option menu (Logout Icon) Inside the activity I have a fragment which is having an option menu too. But the option menu of the fragment is always coming at last. I want it before the parent activity's option menu.

Menu looks like this -

enter image description here


Solution

  • The order of the menus is controlled by the orderInCategory xml attribute, where a smaller order number will appear first (left-most).

    If you are inflating the menu items from XML, set the android:orderInCategory attribute in your Activity menu item to be higher than the one in the fragment.

    If you are setting the menu items at runtime, set a higher order for the activity's menu item than the fragment's when calling menu.add().

    Hope that helps!