Reputation: 8543
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?
Upvotes: 14
Views: 11943
Reputation: 8543
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.
menuCategory
attribute value to "system"
.Activity
) I had to set high orderInCategory numbers, such as 10, 11, 12, 13 etc.orderInCategory
value 1 it showed up as the first item.Upvotes: 24
Reputation: 2690
All is correct, just use below one line code in onCreateView.
setHasOptionsMenu(true);
Upvotes: 0
Reputation: 22306
I'm not sure where you're getting that you can't set the showAsAction option or the icon programmatically ex:
public void onCreateOptionsMenu(Menu menu)
{
menu.add(Menu.NONE, /** group ID.. not really needed unless you're working with groups **/
0, /** this is the items ID (get this in onOptionsItemSelected to determine what was clicked) **/
Menu.NONE, /** ORDER.. this is what you want to change **/
"Cancel") /** title **/
.setIcon(R.drawable.ic_menu_cancel)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
Docs:
menu.add(int groupId, int itemId, int order, CharSequence title)
setShowAsAction(int actionEnum)
Upvotes: 4