2

In my app, I want to show some buttons in a LinearLayout that expands over whole width of the screen. Depending on screen size and/or orientation, I would like to hide those buttons, that don't fit in the row, in some OverflowMenu, similar to the behaviour of the ActionBar. If possible, I would like to describe the buttons in a menu resource file with the ifRoom|always attributes.

I considered displaying a Toolbar from the latest AppCompat library, but that contains too many elements that I don't need and don't know how to turn off.

Is there some library or simple way to do this?

2 Answers 2

5

Here is an example to do what you want. Make sure your ActionMenuView XML item is wrap_content for height and width, then gravity to the right. Surround it in a LinearLayout which takes the whole width and provides background color.

Use this code to initialize the ActionMenuView (obviously you will need to change the button callbacks)

        ActionMenuView actionMenuView = (ActionMenuView) findViewById(R.id.editBar);

        final Context context = this;
        MenuBuilder menuBuilder = new MenuBuilder(context);
        menuBuilder.setCallback(new MenuBuilder.Callback() {
            @Override
            public boolean onMenuItemSelected(MenuBuilder menuBuilder, MenuItem menuItem) {
                return onOptionsItemSelected(menuItem);
            }

            @Override
            public void onMenuModeChange(MenuBuilder menuBuilder) {

            }
        });

        // setup a actionMenuPresenter which will use up as much space as it can, even with width=wrap_content
        ActionMenuPresenter presenter = new ActionMenuPresenter(context);
        presenter.setReserveOverflow(true);
        presenter.setWidthLimit(getResources().getDisplayMetrics().widthPixels, true);
        presenter.setItemLimit(Integer.MAX_VALUE);

        // open a menu xml into the menubuilder
        getMenuInflater().inflate(R.menu.editbar, menuBuilder);

        // runs presenter.initformenu(mMenu) too, setting up presenter's mmenu ref...  this must be before setmenuview
        menuBuilder.addMenuPresenter(presenter, this);

        // runs menuview.initialize too, so menuview.mmenu = mpresenter.mmenu
        actionMenuView.setPresenter(presenter);

        presenter.updateMenuView(true);

For what it's worth, I had to read the support library source code for 8 hours to get this to work. The documentation is garbage.

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

1 Comment

as an update of this answer: Moving to appcompat 23.1.1 I had to hack a bit the imports, and it seems that ActionMenuPresenter is now package local... So I had to create a android.support.v7.widget package in my project and add a PublicActionMenuPresenter class as follow: public class PublicActionMenuPresenter extends ActionMenuPresenter { public PublicActionMenuPresenter(Context context) { super(context); } and use that instead of ActionMenuPresenter
4

ActionMenuView is the part of a Toolbar which specifically controls the actions and overflow part of the Toolbar. In your case, you can use an ActionMenuView alone to implement just the actions/overflow part of the Toolbar:

  1. Add an ActionMenuView to the appropriate place in your XML layout
  2. Retrieve a MenuInflater (easiest way is through an Activity's getMenuInflater() method
  3. Call menuInflater.inflate(R.menu.your_menu, actionMenuView.getMenu()) to inflate your menu into the ActionMenuView

4 Comments

That does sound perfect. However, while I can see some buttons next to the overflow Menu button using the Toolbar, I can't see anything if I change Toolbar into ActionMenuView. There is no error or anything, simply empty space where the menu should be displayed.
Make sure your ActionMenuView is given at least 56x56dp of space. Seems to be a bug with the current version if given a smaller space than that it won't display anything
Right, now I am seeing something - it only works, when its width is not match_parent but, for example, wrap_content. If it is the latter, than I only see two buttons and those three dots for the overflow button, just as it appears in the Toolbar. Is there anything I could hack to make it become wider and display more buttons? (It shows one more button in landscape mode :) )
I am still stuck with this. I tried copying the ActionMenuView into my project and debug it myself, but I couldn't find what's going wrong. Could you give me a hint on how to patch this behaviour? /e: Apparently, the bug has already been filed: code.google.com/p/android/issues/detail?id=78364

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.