Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ActionMenuView?

Since SplitActionBar is no longer supported in Android 5.0, I am trying to use an ActionMenuView to achieve a SplitActionBar effect. But I could not find much information on how to use ActionMenuView.

I know I can add a ActionMenuView in the layout file, but I don't know how to add menu items. It doesn't seem like I could inflate them like I do with SplitActionBar.

Could you give some sample code on how to use ActonMenuView? Thanks!

like image 548
jiu9x9uij Avatar asked Jan 16 '15 16:01

jiu9x9uij


3 Answers

Getting ActionMenuView to display a whole screen's width of icons is a chore. 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.

like image 196
bradsh Avatar answered Nov 08 '22 23:11

bradsh


It seems the API has changed in the meantime. Currently, the following code works:

ActionMenuView actions = new ActionMenuView(activity);

MenuBuilder menuBuilder = (MenuBuilder) actions.getMenu();

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

    @Override
    public void onMenuModeChange(MenuBuilder menuBuilder) {
    }
});

inflater.inflate(R.menu.my_menu, menuBuilder);
like image 10
Kirill Rakhman Avatar answered Nov 09 '22 00:11

Kirill Rakhman


If you are using the v7 appCompat library make sure your activity extends from ActionBarActivity and that you use the support version of the ActionMenuView.

Likewise if you are not using the support library be sure to use the ActionMenuView outside the support library.

From there you can get the ActionMenuView from your layout and populate its menu using the following method:

getMenuInflater().inflate(R.menu.your_menu_here, actionMenuView.getMenu())

If you aren't in an activity where getMenuInflater() is accessible create your own MenuInflater or SupportMenuInflater.

like image 1
George Mulligan Avatar answered Nov 09 '22 01:11

George Mulligan