Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar fragment menu items. Putting them in front

Tags:

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?

like image 968
Eurig Jones Avatar asked Apr 26 '12 15:04

Eurig Jones


People also ask

How to set toolbar in fragment?

To do this, call setHasOptionsMenu(true) in the onCreate() method of the fragment. The Android framework calls in this case the onCreateOptionsMenu() method in the fragment class. Here the fragment can adds menu items to the toolbar.

How do I hide a menu item in the ActionBar?

The best way to hide all items in a menu with just one command is to use "group" on your menu xml. Just add all menu items that will be in your overflow menu inside the same group. Then, on your activity (preferable at onCreateOptionsMenu), use command setGroupVisible to set all menu items visibility to false or true.

How to add Action bar in fragment Android?

To do so, you first have to call setHasOptionsMenu() within the fragment's onCreate() method: setHasOptionsMenu(true); Now your fragments menu items will be displayed in the ActionBar. Of course, if you do so, you have to stop using the same entries in your Activity.


3 Answers

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.
like image 151
Eurig Jones Avatar answered Oct 16 '22 21:10

Eurig Jones


Try using "android:orderInCategory" attribute on menu items

like image 34
EvilDuck Avatar answered Oct 16 '22 21:10

EvilDuck


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)

setIcon(int resId)

like image 4
dymmeh Avatar answered Oct 16 '22 21:10

dymmeh