How to programmatically trigger/click on a MenuItem in Android?

放肆的年华 提交于 2019-11-30 07:59:49

Use the method performIdentifierAction, like this:

menu.performIdentifierAction(R.id.action_restart, 0);

You should manually call your listener, with required item as parameter.

MenuItem actionRestart = (MenuItem) findViewById(R.id.action_restart);
onOptionsItemSelected(actionRestart);

As far as I know, there is no mechanism in the SDK that lets you do this. It's certainly not standard practice to do this sort of thing.

I recommend decoupling your logic from the actual UI as much as possible, so you end up not needing to simulate a click to trigger the action. Since you're a Web developer, this should come fairly easily to you.

In this case, you'd want to refactor the toasts into a separate method (or multiple methods), and then call that both when the menu item is clicked and when you want to trigger it manually.

Alternatively, you could try taking the MenuItem returned by findViewById() and passing it to your handler there. But I have no idea if that'll work.

I would like to share my solution as well here. Instead of trying to click programmatically a menu item, I created a separate method for menu item click, and call it anywhere where I need to click menu item. OnOptionsItemSelected method looks as follows. As you can see I moved click logic to a separate method.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        homeClicked();
    }
    return super.onOptionsItemSelected(item);
}
private void homeClicked(){
    ...
}

Now you can call homeClicked anytime you need to click menu item programmatically.

Ajay Shrestha

Even though it's not the best way to do,

MenuItem item_your_choice;

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.your_menu, menu);
    item_your_choice = menu.findItem(R.id.item_your_choice);
    return super.onCreateOptionsMenu(menu);
}

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case item_your_choice:
           //do whatever you want
            break;
        }
   return super.onOptionsItemSelected(item);
}

just Call from any method

onOptionsItemSelected(item_you_choice);
  1. Make global Menu

    public Menu mMenu;
    
  2. Assign menu to mMenu while override onCreateOptionMenu

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        this.mMenu = menu;
        return super.onCreateOptionsMenu(menu);
    }
    
  3. Call event like this:

    if(mMenu != null) {
        MenuItem action = mMenu.findItem(R.id.action_restart);
    
        if(action != null) {
            onOptionsItemSelected(action);
        }
    }
    

As per your example for menu item above :

<item android:id="@+id/action_restart" android:title="Restart"
        android:orderInCategory="1" />

use callOnClick() method :

((ActionMenuItemView)findViewById(R.id.action_restart)).callOnClick();

There is a standard method to do this -
Create a new instance of MenuItem class and change the overridden method getItemId() to return the id of desired menu item and leave the rest unchanged.

MenuItem actionRestart = new MenuItem() {
                                             @Override
                                             public int getItemId() {
                                               return R.id.action_restart;
                                             }

                                             ...

                                           };
onOptionsItemSelected(actionRestart);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!