Search code examples
actionbarsherlockrefreshprogressandroid-actionbar-compat

How to get Menu object with ActionBarCompat (showing progress bar in ActionBar)


I am switching from Sherlock to AppCompat, and I am used to do this thing where I replace refresh action item with a progress bar while something is loading like this

public void setRefreshButtonState(boolean refreshing) {
    if (mOptionsMenu == null) {
        return;
    }

    final MenuItem refreshItem = mOptionsMenu.findItem(R.id.action_refresh);
    if (refreshItem != null) {
        if (refreshing) {
            refreshItem.setActionView(R.layout.actionbar_indeterminate_progress);
        } else {
            refreshItem.setActionView(null);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    mOptionsMenu = menu;
    return super.onCreateOptionsMenu(menu);
}

But since Menu is the regular menu in AppCompat (unlike Sherlock), it obviously throws

Call requires API level 11 (current min is 9): android.view.MenuItem#setActionView

I tried all the MenuItemCompat static methods, but no luck

Thanks!


Solution

  • I was searching for the wrong thing. You dont want to comapt menu object but the menu item, like this

    MenuItemCompat.setActionView(refreshItem, R.layout.actionbar_indeterminate_progress);