androidmenuandroid-actionbaroptionmenu

ActionBar item does not shown in Option Menu


I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.

This is my onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.opt_menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

This is my opt_menu_main layout:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item
    android:id="@+id/menu_act_settings"
    android:title="Settings" 
    android:icon="@drawable/ic_action_settings"
    android:showAsAction="ifRoom"/>
</menu>

I'm not using support library (my target API is 17).

If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar. I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.

I've also tried with showAsAction="ifRoom|withText"but doesn't work.

I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?

Thanks


Solution

  • For the moment I solved like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"> 
        <item
        android:id="@+id/menu_act_settings"
        android:title="Settings" 
        android:icon="@drawable/ic_action_settings"
        android:showAsAction="always"/>
        <item
        android:id="@+id/menu_settings"
        android:title="Settings" 
        android:showAsAction="never"/>
    </menu>
    

    So in the onOptionsItemSelected method :

    public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) { 
            case R.id.menu_add_schedule:
            case R.id.menu_act_add_schedule:
                    //some code
                break; 
            }
        return true; 
    }
    

    I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar, since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.

    The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.

    This behavior can not be obtained with showAsAction="always" .

    It would be great if someone could find a better solution.

    Thanks anyway.