Android – ActionBar fragment menu items. Putting them in front

android

I'm adding fragment menu items using the onCreateOptionsMenu successfully…

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.additional_fragment_menu, menu);
}

This additional menu above includes 1 menu item. What I need to do is put this menu item in front of all other existing items so that it appears first on the actionbar. Currently it appears last.

(This is an android:showAsAction="always" item I'm adding)

I tried adding it programmatically, but there is no option in the MenuItem object to allow you to specify icon and showAsAction flags.

Anyone have any ideas?

Best Answer

Ok I cracked this myself with a pointer in the right direction from EvilDuck. And yes dymmeh you're right you can do this programmatically!!

What I needed to use was a combination of orderInCategory and menuCategory. Android seems to ignore orderInCategory if you don't have a menuCategory specified.

  • I specified the category of every single menu item in my app by setting the menuCategory attribute value to "system".
  • My base menu items (the ones that are loaded via the Activity) I had to set high orderInCategory numbers, such as 10, 11, 12, 13 etc.
  • When I then inflated and added the additional fragment menu xml with the same category and an orderInCategory value 1 it showed up as the first item.