12

How to show Indeterminate ProgressBar when Refresh button is pressed in ActionBarSherlock and again show Refresh Button when ViewGroup on refreshed?

Update 1: I have a answer here which is incomplete. I am placing a bounty on question so that more developers can help build a good answer which can useful to others in future.

How can we show a Indeterminate ProgressBar which looks like the one shown in the image belowenter image description here

Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • 1
    It looks like you had a question that @anadobes answered to your satisfaction (and which you should accept). Now you have another, independent, question which you should ask as a new question instead of as an edit to this one. If you'd like the bounty on the new question, you can flag this question, explain the situation, and a mod might refund it for you. – blahdiblah Jun 08 '12 at 18:29
  • 1
    I think the edit is close enough that it doesn't warrant a new question. But, in future, please try to include this kind of information *when asking the original question*. We occasionally have issues with users attempting to re-use existing questions to ask new ones, which is not allowed. –  Jun 08 '12 at 20:20
  • Why do it in the first place? This does not confirm to the current design patterns – Bostone Jun 08 '12 at 22:06
  • @Bostone A little more detail on your comment. – Gaurav Agarwal Jun 09 '12 at 13:06
  • I've created solution on another answer, if you're interested in: http://stackoverflow.com/a/14360958/492624 – Marek Sebera Jan 16 '13 at 14:47

3 Answers3

31

It seems like ActionBarSherlock doesn't provide specific method to animate a refresh MenuItem.

What you can do (by using classic android API) is to use the setActionView(int resId) method and give the id of a layout with a ProgressBar in it.

At the beginning of your refresh action just call :

item.setActionView(R.layout.refresh_menuitem);

And when your refresh action is finished call :

item.setActionView(null);

Here is a sample of what your layout file refresh_menuitem.xml can have :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:addStatesFromChildren="true"
              android:focusable="true"
              android:paddingLeft="4dp"
              android:paddingRight="4dp"
              android:gravity="center"
              style="?attr/actionButtonStyle">
    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="true"
            style="@android:style/Widget.ProgressBar.Small"/>
</LinearLayout> 
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
gurvanhenry
  • 598
  • 4
  • 9
  • set the ProgressBar style to "@android:style/Widget.Holo.ProgressBar.Small" – omoling Oct 29 '12 at 22:08
  • 3
    Well, when I try to use the above code, the `ProgressBar` appears in the left side of `ActionBar` and list navigation spinner becomes invisible. Do you have any idea on how to place this `ProgressBar` at the same place as the `MenuItem`? – jaibatrik Nov 29 '12 at 07:21
  • I have the same issue as above, jaibatrik you ever find an answer to this? – Lion789 Mar 04 '14 at 02:09
10

Here is how you add this kind of indeterminate ProgressBar with a ActionBarSherlock object : (actually it's easier the other one but the progressBar is shown alone and not above a MenuItem)

1 - Put this line in the onCreate() method before the setContentView() call :

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

-> This line specify that you will use the indeterminate ProgressBar function.

2 - Enable the indeterminate ProgressBar by calling :

setSupportProgressBarIndeterminateVisibility(true);

3 - Disable the indeterminate ProgressBar by calling :

setSupportProgressBarIndeterminateVisibility(false);

Remark : Have a look in the sample folder of the ActionBarSherlock folder. I found this code in the following file : JakeWharton-ActionBarSherlock-9598f2b\samples\demos\src\com\actionbarsherlock\sample\demos\IndeterminateProgress.java

gurvanhenry
  • 598
  • 4
  • 9
2

Here is a complete code:

private static final int menuItemIdRefresh = 10; //class constant
private boolean refresh;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem refreshItem = menu.add(0, menuItemIdRefresh, 0,
            getString(R.string.action_refresh)).setShowAsActionFlags(
            MenuItem.SHOW_AS_ACTION_ALWAYS);

    if (isRefreshing) {
        refreshItem.setActionView(R.layout.indeterminate_progress);
    } else {
        refreshItem.setActionView(null);
        refreshItem.setIcon(R.drawable.ic_refresh);
    }

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case menuItemIdRefresh: {
            //the user has pressed the refresh button
        if (!isRefreshing) {
            isRefreshing = true;
            new RefreshMyViewAsyncTask().execute("");
        }
    }
        break;

    default:
        break;
    }

    return false;
}

One last note, in order to get the above code working you´ll need also call supportInvalidateOptionsMenu(). You can add that to the RefreshMyViewAsyncTask's onPreExecute() method.

Hope this helps to somebody.

Carlos
  • 1,319
  • 2
  • 17
  • 20