Put a progressBar on ActionBar

AndroidAndroid ActionbarActionbarsherlock

Android Problem Overview


I am trying to put an indeterminate ProgressBar on the actionBar. I was using an actionView to put the progressBar like Google+ app for example.

<item
    android:id="@+id/menu_progress"
    android:actionLayout="@layout/action_progress"
    android:menuCategory="container"
    android:showAsAction="always">
</item>

the problem is that the progress bar is considered as an item and therefore on a Nexus S portrait mode I have only one other item on the actionbar while on Google+ I can see two items plus the progressBar. How is it possible to put a progressbar using the android actionbar?

Android Solutions


Solution 1 - Android

NOTE: The functionality below is now deprecated in the Support Library.

You need to call

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)

in your onCreate() before setting the activity's layout:

e.g.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    ... // set layout etc

> If you are using the support library replace requestWindowFeature with supportRequestWindowFeature

And then call

setProgressBarIndeterminateVisibility(true);

on your Activity whenever you want to show the progress spinner.

Solution 2 - Android

My situation required updating the progress bar from a Fragment using the Android Support Library version 4.

In my "MainActivity extends ActionBarActivity" as suggested by Jokeefe:

supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

before

setContentView(R.layout.activity_main);

In my fragment's long running task:

onPreExecute

actionBarActivity.setSupportProgressBarIndeterminateVisibility(true);

onPostExecute

actionBarActivity.setSupportProgressBarIndeterminateVisibility(false);
menu.clear();
actionBarActivity = (ActionBarActivity)getActivity();
actionBarActivity.supportInvalidateOptionsMenu();

Not sure if this answers the OP but this is what worked for me based on the posts above. Hope this helps.

Solution 3 - Android

I have found an easiest one to show progress exactly how you need . I found it here . Just use one class and place your progress bar wherever you want.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatroskaView Question on Stackoverflow
Solution 1 - AndroidKuffsView Answer on Stackoverflow
Solution 2 - AndroidHostMyBusView Answer on Stackoverflow
Solution 3 - Androidparambir singhView Answer on Stackoverflow