Home » Android » android – Show progress spinner (refresh) on ActionBar?

android – Show progress spinner (refresh) on ActionBar?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m using the ActionBar. I’d like to have a refresh progress spinner on the titlebar, if I set it to spinning – otherwise hide it. Is that possible?:

// My menu has a refresh item, but it shouldn't be visible on the
// actionbar unless it's spinning.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/menu_refresh"
    android:title="@string/refresh"
    android:icon="@drawable/ic_action_refresh" />
</menu>

...

// When I need to show some work being done on my activity,
// can I somehow now make the spinner associated with the
// refresh item become visible on the action bar?
getActionBarHelper().setRefreshActionItemState(true);

I don’t want it on the ActionBar unless it’s “in progress” / spinning.

Thanks

How to&Answers:

Apologies for no code tags, posting from phone…

This is from ActionbarSherlock (Google that if you’ve not come across it, allows actionbar support in pre honeycomb)

In onCreate of main activity

// This has to be called before setContentView and you must use the 
// class in android.support.v4.view and NOT android.view

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

To show/hide progress in action bar. Notice with actionbarsherlock you must use boolean.TRUE/FALSE, not just true/false………

if (getSupportLoaderManager().hasRunningLoaders()) {
   setProgressBarIndeterminateVisibility(Boolean.TRUE); 
} else {
   setProgressBarIndeterminateVisibility(Boolean.FALSE); 
}

Answer:

If you extends from a ActionBarActivity, try this:

public class MainActivity extends ActionBarActivity {

    boolean showUp=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setSupportProgressBarIndeterminateVisibility(Boolean.TRUE);

        Button b = (Button) findViewById(R.id.myButton);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(showUp){
                    setSupportProgressBarIndeterminateVisibility(Boolean.FALSE);
                }else {
                    setSupportProgressBarIndeterminateVisibility(Boolean.TRUE);
                }
                showUp=!showUp;
            }
        });
}