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?

Matroska
  • 6,885
  • 14
  • 63
  • 99
  • https://guides.codepath.com/android/Handling-ProgressBars explains this very nicely – png Mar 18 '15 at 09:04

3 Answers3

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.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • 1
    I have tried this, the problem is that the progress bar appears big and I want to make it small. I tried to put @android:style/Widget.ProgressBar.Small as style but it doesn't work! If I make it working, this would be the perfect solution – Matroska Feb 06 '12 at 09:37
  • If the supported method is not what you want you would need to implement it yourself like this one http://stackoverflow.com/questions/6857364/how-can-i-make-a-custom-progress-bar-in-the-action-bar – Kuffs Feb 06 '12 at 09:42
  • 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_PROGRESS – Matroska Feb 06 '12 at 10:08
  • I have put a style on a progress bar as asked here http://stackoverflow.com/questions/9162481/syling-indeterminate-progressbar-on-actionbar/9162700#9162700 and it works fine :) – Matroska Feb 06 '12 at 17:13
  • If you want to have a progress bar with proper size and padding, check my solution here: http://stackoverflow.com/questions/15519818/indeterminateprogressbar-in-actionbar-styling-padding-issue#answer-18367927 – petrnohejl Aug 21 '13 at 21:32
  • 2
    Updated the answer to reflect `ActionBarActivity` support. – gunar Sep 04 '13 at 09:01
  • 42
    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. – jokeefe Oct 04 '13 at 04:10
  • 4
    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. – Phantômaxx Jul 04 '14 at 16:07
  • And then obviously set it to false **when done** getActivity().setSupportProgressBarIndeterminateVisibility(true); – ymerdrengene Oct 09 '14 at 16:55
  • is it possible to set the progressBar's style to small? – Muhammad Babar Dec 08 '14 at 14:41
  • How to make this support in api level 21. – user2273146 Feb 06 '15 at 11:00
  • 1
    `requestWindowFeature` should be called before `super.onCreate(savedInstanceState);` – Jorge Arimany Aug 05 '15 at 10:02
  • @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. http://developer.android.com/reference/android/view/Window.html#requestFeature(int) – Kuffs Aug 05 '15 at 12:50
  • @Kuffs that's correct unless you use AppCompat library, in that case you it's necessary to call `requestFeature()` before `super.onCreate()` – Jorge Arimany Aug 06 '15 at 10:23
  • Please post link to the documentation stating that. – Kuffs Aug 06 '15 at 13:12
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.

HostMyBus
  • 195
  • 2
  • 9
  • what if you don't write this: menu.clear(); actionBarActivity = (ActionBarActivity)getActivity(); actionBarActivity.supportInvalidateOptionsMenu(); – Karioki Mar 19 '15 at 10:17
  • 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. – HostMyBus Mar 23 '15 at 20:21
-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.

parambir singh
  • 224
  • 2
  • 13