0

I have a NavigationDrawer and hide/show the OptionsMenu (which is located on the bottom due to a split ActionBar) whenever the drawer gets opened/closed:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!isDrawerOpened) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    return false;
}

I would like to hide the Menu with an animation, (for reference: like the animation used by the system when you call ActionBar.hide() ), however I found no way to access the Layout used for the Menu (so I could get a View object to apply the animation to). It seems there's no public API available to perform this, I wouldn't mind some reflection approach if I knew what to look for. Any suggestions?

UPDATE

looking at this source I finally figured out how to access the View of a split ActionBar using a non-public identifier:

    private View getSplitActionBarView() {
    Window window = getWindow();
    View v = window.getDecorView();
    return v.findViewById(getResources().getIdentifier("split_action_bar",
            "id", "android"));
}

Now I can create an Animation, set an AnimationListener and call invalidateOptionsMenu() as soon as the Animation ends. There's one problem left: doing this will let an ugly white rectangle at the place where the bottom part of the ActionBar was, this white rectangle will stay there until the Animation is complete. To get rid of it I would need to use <item name="android:windowActionBarOverlay">true</item> in my style XML (or doing the same by code) to display the ActionBar in overlay mode. However this is not what I need.

The question is now: is there any way to apply the overlay mode only to the split ActionBar (= the bottom part of the ActionBar)?

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • Well...inflate()...creates the view...View view = getMenuInflater().inflate(...)... – ElDuderino Apr 10 '14 at 22:07
  • @ElDuderino that was the 1st thing I was thinking of, but.. `void android.view.MenuInflater.inflate(int menuRes, Menu menu)` – Droidman Apr 11 '14 at 12:04
  • You can set an ActionView to the menu item, you can get the ActionView in onPrepareOptionsMenu and run your animation. – ElDuderino Apr 11 '14 at 12:40
  • @ElDuderino yes I could probably access the `View` of a single `MenuItem`, but I need to apply the animation to the whole menu bar – Droidman Apr 11 '14 at 14:18
  • 1
    http://stackoverflow.com/a/21125631/2001247, maybe this will help you, I don't know much about the split actionbar...but it's a nice question, please post an answer if you can get it to work :) – ElDuderino Apr 12 '14 at 09:35
  • @ElDuderino thanks for linking that, now I was able to get the `View` of the split `ActionBar`, I updated my question – Droidman Apr 12 '14 at 13:33
  • You say it works with android:windowActionBarOverlay, a quick fix could be to use android:windowActionBarOverlay and set the top margin of your layout to the height of the actionbar...this way it would look like a normal actionbar and your anim could be working... but yeah, this is not a nice solution :) – ElDuderino Apr 12 '14 at 15:29
  • @ElDuderino yep this is an option. However I gave up the idea, since you can't achieve a 2-way animation anyway. After calling `invalidateOptionsMenu()` and hiding the bottom AB part with an animation, you will need to call it again to get the `Menu` back. In this case it appears immediately and there's no way to set an animation. The only way out would be using an own `View` instead of an `OptionsMenu`, it's not worth it however. – Droidman Apr 12 '14 at 16:33

1 Answers1

0

You need to call invalidateOptionsMenu() when the drawer is open/close, like this

public void onDrawerOpened(View view) {
     invalidateOptionsMenu();
}

Now, onPrepareOptionsMenu() will be called,where you can hide/show the option menu like this..

@Override
public boolean onPrepareOptionsMenu(Menu menu){
    // check if drawer is open
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);

    for(int index = 0 ; index < menu.size() ; index ++){
        MenuItem menuItem = menu.getItem(index);
        if(menuItem != null) {
            // hide the menu items if the drawer is open
            menuItem.setVisible(!drawerOpen);
        }
    }
    return super.onPrepareOptionsMenu(menu);
}

You can hide all the menu or any of the required menu item.

Libin
  • 16,967
  • 7
  • 61
  • 83
  • 1
    I guess you misunderstood the question.. I know how to hide the `Menu`, I asked how to hide it with a translate `Animation`. – Droidman Apr 11 '14 at 12:03