Question

I am creating an app with 4 tabs using fragments. I am trying to add menu items to the bar above the tabs. When I run the app, the tabs show up however nothing is where the menu items should be.

The xml for the menu:

<item
    android:id="@+id/action_settings"
    android:showAsAction="always"
    android:title="@string/action_settings"
    android:icon="ic_action_settings">
    <menu>
        <item
            android:id="@+id/help"
            android:title="@string/help"/>
        <item
            android:id="@+id/about"
            android:title="@string/about"/>
    </menu>
 </item>

In the main class where I create the tabs, I extend Activity and add the tabs using ActionBar.Tab.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.start, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
        case R.id.action_settings:
          return true;
        default:
          return super.onOptionsItemSelected(item);
      }
}

I inflate the menu only in the main class, do I have to do this in each of the tabs for the menu items to show up or is there something else I need to do? Thanks in advance for the help!

Was it helpful?

Solution

Well you are trying to nest a menu inside an item tag in your main.xml

Try this instead:

     <item android:id="@+id/action_settings"
        android:showAsAction="always"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never"/>
    <item android:id="@+id/action_b"
        android:id="@+id/help"
        android:title="@string/help"
        app:showAsAction="never"/>
    <item android:id="@+id/action_c"
        android:id="@+id/about"
        android:title="@string/about"
        app:showAsAction="never"/>

OTHER TIPS

You have to specify that your Fragment has an options menu. Add this to your Fragments:

@Override
public void onCreate( Bundle savedInstanceState ) {

    super.onCreate( savedInstanceState );
    setHasOptionsMenu( true );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top