2

I'm creating action bar menu in my main activity:

public boolean onCreateOptionsMenu(Menu menu) {
    // Used to put dark icons on light action bar
    SubMenu sub = menu.addSubMenu(null);
    sub.add(0, R.style.Theme_Sherlock, 0, "Default");
    sub.add(0, R.style.Theme_Sherlock_Light, 0, "Light");
    sub.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0,
            "Light (Dark Action Bar)");

    MenuItem ShareButton = sub.getItem();
    ShareButton.setIcon(R.drawable.ic_action_overflow);
    ShareButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
            | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;

}

And then in my fragment activity, I try to change menu for second fragment:

first i do this:
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

And I do this:

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // TODO Auto-generated method stub
        //final MenuInflater inflater2 = getMenuInflater();
        //inflater.inflate(R.menu.historymenu, menu);
        Log.v("Some Log", "I'm in on Create Options Menu");

        SubMenu sub = menu.addSubMenu(null);
        sub.add(0, R.style.Theme_Sherlock, 0, "HistoryCheck");


        MenuItem ShareButton = sub.getItem();
        ShareButton.setIcon(R.drawable.ic_action_overflow);
        ShareButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        super.onCreateOptionsMenu(menu, inflater);
    }

As you can see, I'd love to inflate menu from .xml for each fragment and main activity but for some reason inflation does nothing.

With code to add new submenu, I get 2 menus when I switch to second tab/fragment, and I'd love to change existing menu, not to add additional one.

Please help me to change correct line.

Tnx.

1 Answer 1

11

If you want the menu to change, don't add one in your main activity, add the menu only in your fragment. That way when you switch fragments, the menu will switch accordingly.

You CAN inflate the menu from xml:

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

Just make sure you import com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuInflater.

the_menu.xml (with submenu)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_item"
        android:showAsAction="always"
        android:title="Menu">
        <menu>
            <item
                android:id="@+id/submenu_1"
                android:showAsAction="always"
                android:title="submenu 1"/>
            <item
                android:id="@+id/submenu_2"
                android:showAsAction="always"
                android:title="submenu 2"/>
        </menu>
    </item>

</menu>
3
  • If you take a look at my code, I'm trying to add menu in "action_overflow" "pipe like icon" on the upper right hand side. If I inflate menu like this it simply does not appear, and no error occur. Like menu is hidden behind tabs or somewhere.
    – Balkyto
    Sep 18, 2012 at 9:56
  • I've made some mistake in main activity, then while correcting it i deleted setHasOptionsMenu(true); that was the reason why it was not working. You helped as usual :) Tnx Benito!
    – Balkyto
    Sep 19, 2012 at 8:09
  • 1
    be sure to add setHasOptionMenu(true) in the fragment's onCreate()
    – Robert
    Nov 7, 2013 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.