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!
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.
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);
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With