Replacing an ActionBar menu item icon with an indeterminate ProgressBar

Melllvar picture Melllvar · Aug 22, 2011 · Viewed 13.2k times · Source

I would like to add an indeterminate progress bar to the Honeycomb ActionBar, so that any time the user presses "Refresh", the refresh icon temporarily turns into an indeterminate progress bar, until the task completes. The Email app does this already, but I can't figure out how.

Any advice?

Answer

larham1 picture larham1 · Nov 11, 2012

To clarify Jon O's answer, the key is to set and unset an action view on the refresh action. This works in both ActionBarSherlock and native 4.x action bar. The following snippet will put the progress indeterminate view on top of the refresh icon, assuming the refresh menu item has ID 'refresh_option' and the replacement layout (which has a ProgressBar) is in layout 'progress_wheel':

 MenuItem item = abmenu.findItem(R.id.refresh_option);
 LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View abprogress = inflater.inflate(R.layout.progress_wheel, null);
 item.setActionView(abprogress);

Unset the progress view, and the refresh icon will return to visibility:

 item.setActionView(null);

See a more detailed example on github.