Put a progressBar on ActionBar
Asked Answered
D

3

52

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?

Diathesis answered 6/2, 2012 at 8:42 Comment(1)
guides.codepath.com/android/Handling-ProgressBars explains this very nicelyMultivalent
H
166

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.

Herbage answered 6/2, 2012 at 9:18 Comment(15)
I have tried this, the problem is that the progress bar appears big and I want to make it small. I tried to put <item name="abProgressBarStyle">@android:style/Widget.ProgressBar.Small</item> as style but it doesn't work! If I make it working, this would be the perfect solutionDiathesis
If the supported method is not what you want you would need to implement it yourself like this one #6857864Herbage
with that suggestion I will consume one space on the bar. I want to understand just why cannot put a custom progressbar (just different size) using FEATURE_INDETERMINATE_PROGRESSDiathesis
I have put a style on a progress bar as asked here #9162981 and it works fine :)Diathesis
If you want to have a progress bar with proper size and padding, check my solution here: #15520318Naissant
Updated the answer to reflect ActionBarActivity support.Butte
For anyone else who's trying to get this to work from a Fragment, call requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); from the containing Activity's OnCreate() and then call getActivity().setSupportProgressBarIndeterminateVisibility(true); from your Fragment.Gonfalonier
To correct the above comment, requestWindowFeature(Window... should be changed to supportRequestWindowFeature(Window... to have it working. Well, this trick fixed it for me, at least.Elliott
And then obviously set it to false when done getActivity().setSupportProgressBarIndeterminateVisibility(true);Pulcheria
is it possible to set the progressBar's style to small?Leahy
How to make this support in api level 21.Loreeloreen
requestWindowFeature should be called before super.onCreate(savedInstanceState);Shatterproof
@Jorge. That is incorrect. It needs to be called before setContentView() but can be called after the super call to onCreate(). Both points were illustrated in my answer. developer.android.com/reference/android/view/…Herbage
@Herbage that's correct unless you use AppCompat library, in that case you it's necessary to call requestFeature() before super.onCreate()Shatterproof
Please post link to the documentation stating that.Herbage
A
0

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.

Alger answered 23/2, 2015 at 20:37 Comment(2)
what if you don't write this: menu.clear(); actionBarActivity = (ActionBarActivity)getActivity(); actionBarActivity.supportInvalidateOptionsMenu();Torchwood
Sorry for the delayed response. Without those last three lines the bar does not update with the content I need to change to and is empty.Alger
K
-1

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.

Knowledgeable answered 2/10, 2017 at 3:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.