Search code examples
androidandroid-actionbarandroid-4.0-ice-cream-sandwich

ICS ActionBar overflow menu item showing up on both top ActionBar and overflow menu


On a Galaxy S3, I have an issue where a button in the ActionBar is configured as showAsAction="always" and being shown on both the hardware overflow menu and the Action buttons. I would like it to not be shown on the hardware overflow menu and only on the Action buttons. I can disable the menuitem in the onCreateOptionsMenu but it will hide the button on both places.

Something to note: if I force the "3 dots" Action Overflow menu to show, the refresh button gets properly hidden from the hardware overflow menu but still doesn't get hidden from the hardware overflow menu.

Something else to note: if I call menu.size() in either onCreateOptionsMenu or onPrepareOptionsMenu, it doesn't reflect the extra button. For example, I have four buttons and the first button is being shown in both the Action buttons and the overflow menu. menu.size() still returns 4 and doesn't seem to realize that it is showing an extra button.

I can't post a screenshot because this is an app for a client but here is my actionbar.xml file. The refresh button shows in both the overflow and the action bar at the top.

actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/refreshmenuitem"
          android:icon="@drawable/refreshicon"
          android:title="Refresh"
          android:showAsAction="ifRoom" 
          android:visible="true"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          />
    <item android:id="@+id/helpbutton"
        android:title="Help"
        android:showAsAction="collapseActionView"
        android:icon="@drawable/ic_action_helpbutton" />
    <item android:id="@+id/settingbutton"
        android:title="Settings"
        android:icon="@drawable/ic_action_settingbutton"
        android:showAsAction="collapseActionView" />
    <item android:id="@+id/importbutton"
        android:title="Import file"
        android:icon="@drawable/ic_action_importbutton"
        android:showAsAction="collapseActionView" />
</menu>

Solution

  • So I figured it out but it is kind of a hack. So if you have a phone such as the Samsung Galaxy S3 that has a hardware menu button, the onCreateOptionsMenu function actually gets called twice. Once for when the Activity gets loaded to load any menu items that should display in the top right and another time when the user presses the hardware menu button. All I did was create a variable called _firstTimeOnCreateOptionsMenu that is set to false after the first time it is onCreateOptionsMenu method:

        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Populates the actionbar/Menu
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.actionbarmenu, menu);
            boolean hardware = false;
    
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
            {
                hardware = ViewConfiguration.get(context).hasPermanentMenuKey();
            }
    
            MenuItem b1 = menu.findItem(R.id.refreshmenuitem);
    
            if(!hardware || _firstTimeOnCreateOptionsMenu)
            {
                b1.setVisible(true);
                _firstTimeOnCreateOptionsMenu = false;
            }
            else
            {
                b1.setVisible(false);
                _firstTimeOnCreateOptionsMenu = true;
            }
        }
    

    Hopefully this helps anyone else who is having this issue.