Android get a View reference to action bar item?

You can do menu.findItem(R.id.youritemid) to get the menu item, and you can get reference to menu object in your onCreateOptionsMenu(Menu menu) method then you can initialize a global variable with that menu object to use it anywhere.

Here is some code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    _menu = menu;
    return true;
}

I am initialize my global variable Menu _menu; with menu object and then i can do like this MenuItem mi = _menu.findItem(R.id.itemid); any where i want.

Edit:
Please take care that you are not calling anything on menuitem before the menu gets created, you can schedule a thread to wait for 3 to 5 secs or you can do it some other way, all you should worry about is whether the menu has been initialized or not.

Leave a Comment