1

I have group inside menu.xml and I try to change the visibility of the group.

  //in the onCreate
  myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
  setSupportActionBar(myToolbar);

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.setGroupVisible(R.id.group, showGroup);
    return true;
  }

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

This doesn't change the visibility of the group when I open the app.(when I call to invalidateOptionsMenu).(it change the visibility only of the items inside the three dot menu.. not the drawables icons). Only if I click the menu three dot it starts to work as I want. If I open the app and show dialog and close it the menu works ok..

How can I fix it to work when I open the app?

Thanks

Edit


    <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
    android:id="@+id/group"
    >
<item
    android:id="@+id/delete_btn"
    android:icon="@drawable/ic_delete_black_24dp"
    android:title="delete"
    app:showAsAction="always"
  />

<item
    android:id="@+id/invite_btn"
    android:icon="@drawable/ic_group_add_black_24dp"
    android:title="invite"
    app:showAsAction="never"
    />
</group>
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
    android:title="Sign Out"
    app:showAsAction="never"/>

</menu>

if I run this code:

this.showGroup=true;
invalidateOptionsMenu();

it shows only the invite_btn inside the 3-dots.

when I click the 3-dots it render the icon of the delete_btn.

In this time if I run this code

 this.showGroup=false;
invalidateOptionsMenu();
.....
this.showGroup=true;
invalidateOptionsMenu();
.....

every thing works well.

Alon
  • 2,919
  • 6
  • 27
  • 47
  • Can you provide screenshots of what you mean. It's a bit difficult to visualize what you mean – Passer by Aug 20 '16 at 18:42
  • Or better yet, it might be better to hide the menu entries individually: `menu.findItem(R.id.menuItem1).setVisible(showGroup);` I think the group visibility only works when the overflow menu is opened. If that's what you meant, then I can add it in as an answer so you may **accept** it. – Passer by Aug 20 '16 at 18:45
  • @Passerby this is the same as before... – Alon Aug 20 '16 at 18:51
  • only when the app change the focus from the activity and return every thing start to works as expected – Alon Aug 20 '16 at 19:54
  • I tested your `menu.xml` and code and that seems to work okay for me. What version of the `com.android.support:appcompat-v7` are you running? What's your Android version, and your testing device? – Passer by Aug 20 '16 at 21:06
  • I am testing on the emulator android .6. com.android.support:appcompat-v7:24.1.1 – Alon Aug 20 '16 at 21:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121441/discussion-between-passer-by-and-alon). – Passer by Aug 20 '16 at 21:13

1 Answers1

0

The solution for this was to upgrade the emulator. Maybe a bug in the emulator. When I tried it on some devices it works ok.

Alon
  • 2,919
  • 6
  • 27
  • 47