Search code examples
androidandroid-support-libraryandroid-actionbar-compat

SupportActionBar not setting visibility as I want


I'm using ActionBar from support library. I want to change the visibility of one MenuItem on runtime. In most cases this works fine, but sometimes, hiding does not work as expected. I am using the following code to change the visibility:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    boolean show = /* some condition */
    myMenuItem.setVisible(show); // I got myMenuItem from onCreateOptionsMenu()
    return superRetVal;
}

As I said, this works fine for most cases. But if I make a FragmentTransaction and then call invalidateOptionsMenu(), it sometimes gets an error. The error is that the icon itself is hidden because show == false, but the space is still blocked (like when setting View.setVisibility(View.INVISIBLE)). In the screenshow below, you may see the space is still blocked. I have highlighted the unused, but blocked space with a red box.. After this error occurs once, I can only get rid of it by clicking on the overflow menu. Until then, no MenuItem.setVisible() will have any effect.

ActionBar

What's the problem here?
Thanks!

Edit
The item menu_to_current is causing the problem:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:malaka="http://schemas.android.com/apk/res-auto" >

<item 
    android:id="@+id/menu_to_current"
    android:orderInCategory="100"
    android:title="To Current"
    android:icon="@drawable/ic_menu_current"
    malaka:showAsAction="always"
    android:visible="false"/>

<item 
    android:id="@+id/menu_show_buttons"
    android:orderInCategory="100"
    android:title="Show Buttons"
    malaka:showAsAction="never"/>

<item  
    android:id="@+id/menu_settings"
    android:orderInCategory="100"
    android:title="Settings"
    android:icon="@android:drawable/ic_menu_preferences"
    malaka:showAsAction="never"/>

</menu>

Edit2
I was testing on emulator api 16. I now additionally tested on real device api 10, real device api 18, emulator api 18. And the error only occurs on emulator api 16. Unfortunately I don't have a real device api 16 available to test that.


Solution

  • I had the possibility to set up new emulator on another machine. The error did not occur; this problem was caused by a problem of the emulator I worked with. Thanks for your responses, though.