1

Is it possible to programmatically detect when a menu is overflowed?

My intention is to have a menu item always be visible (SHOW_AS_ACTION_ALWAYS), except for in the case where it would cause other items to overflow, in which case, don't show the menu item at all. That is:

if (overflowed) actionBarMenu.removeItem(id);

1 Answer 1

1

You are not saying where this menu is appearing, so I'll just give an example of what you can do with a Toolbar. What you need to do is to get the reference to the ActionMenuView from the Toolbar and then call isOverflowMenuShowing on it, something like this:

private boolean isOverflowShowing(Toolbar toolbar) {
    if(toolbar == null) {
        return false;
    }

    for(int i = 0; i < toolbarView.getChildCount(); i++) {
        View v = toolbarView.getChildAt(i); 
        if(v instanceof ActionMenuView) {
            return ((ActionMenuView)v).isOverflowMenuShowing();
        }
    }
    return false;
}

This is crude and dirty - and I haven't tested it - but it should get you started.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.