17

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!

Dmitry
  • 14,306
  • 23
  • 105
  • 189
jiu9x9uij
  • 323
  • 1
  • 2
  • 11
  • 2
    Did you figure it out ? there is lack of resources on that. – osayilgan Mar 06 '15 at 22:37
  • @osayilgan I switched to other method for this part eventually. But you can take a look at George's answer. Please let me know if it works, if so I would mark it as the answer. Thanks :) – jiu9x9uij Mar 16 '15 at 04:48
  • I found another solution using resources from AppCompat Library Theme. With certain attributes, you can style the ActionView with background color and etc. – osayilgan Mar 16 '15 at 17:21

5 Answers5

15

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.

bradsh
  • 216
  • 3
  • 3
10

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);
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
1

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.

George Mulligan
  • 11,813
  • 6
  • 37
  • 50
0

In appcompat-v7:27.0.2, the ActionMenuView requires a minimum width of 56dp. Do not use android:layout_width="wrap_content".

If your popup theme is being ignored, make sure you call setPopupTheme(int) before any call to getMenu() on ActionMenuView.

owjsub
  • 231
  • 2
  • 4
0

Inflate

you can use this code in your activity :

menuInflater.inflate(R.menu.{your_menu_res_id}, {your_ActionMenuView_instance}.menu)

like this :

menuInflater.inflate(R.menu.settings_menu, settings_menu.menu)

Item Click Listener

then you can add item click listenter :

{your_ActionMenuView_instance}.setOnMenuItemClickListener()

like this :

settings_menu.setOnMenuItemClickListener { menuItem ->
        when (menuItem.itemId) {
            R.id.menu_settings_save -> {
                // your code

                return@setOnMenuItemClickListener true
            }
            else -> return@setOnMenuItemClickListener false
        }
    }
aam01b
  • 1