Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show progress spinner (refresh) on ActionBar?

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

like image 200
user291701 Avatar asked Jan 30 '12 02:01

user291701


2 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); 
}
like image 147
MartinS Avatar answered Nov 19 '22 00:11

MartinS


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;
            }
        });
}
like image 25
Victor Ruiz. Avatar answered Nov 19 '22 01:11

Victor Ruiz.