9

I'm trying to do add a progressbar in code and make it determinate:

progressBar = new ProgressBar(getActivity());
progressBar.setLayoutParams(layoutParams);
parent.addView(progressBar, index);
progressBar.setId(id.list_item_secondary);
progressBar.setProgressDrawable(getResources().getDrawable(drawable.progress_horizontal));
progressBar.setIndeterminate(false);
progressBar.setMax(100);

After progressBar.setIndeterminate(false), isIndeterminate is still true and the progress keeps shows the indeterminate circle.

How can I make it determinate?

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277

4 Answers4

2

From the ProgressBar source code here, the constructor you are calling is in line 237 which is calling the constructor in line 241 which in turn calls the constructor in line 245 with the style:

com.android.internal.R.attr.progressBarStyle

This style has the attribute android:indeterminateOnly set to true by default so your calls to setIndeterminate are ignored. See the function description at line 433.

I haven't done this but I assume that if you call the constructor in line 245 like this:

progressBar = new ProgressBar(getActivity(), null, <Your Style ID>);

passing as the third parameter a style definition with android:indeterminateOnly to false it should work. Based in the source code I assume that setIndeterminate is there only to enable it and not to disable it.

Hope this helps...

ChD Computers
  • 3,135
  • 3
  • 23
  • 33
  • Do you know of a such a style in Android's framework? – AlikElzin-kilaka Jul 22 '13 at 16:57
  • @kilaka I think that android.R.style.Widget_ProgressBar_Horizontal will do the job... – ChD Computers Jul 22 '13 at 19:15
  • It wasn't enough. I had to set the progress drawable: `progressBar.setProgressDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal))` – AlikElzin-kilaka Jul 23 '13 at 05:23
  • Summarizing: indeterminateOnly is set by default to true, which prevents from changing to determinate. This can be changed only by a style, which is set only in the constructor. – AlikElzin-kilaka Jul 23 '13 at 05:36
  • Side note: The line numbers you wrote above are different from one Android version to another. You are referencing the master branch. A lot of phones aren't running the master but one of the release branches. Also, the master can change and you line numbers won't be relevant any more. – AlikElzin-kilaka Jul 23 '13 at 05:37
  • Also, even when providing with a newer, built-in, style (`Widget_Holo_Light_ProgressBar_Horizontal`), the progress looks yellowish and old. Here's the drawables needed for the new, blueish, look: http://ppl.ug/cAHLkaL7aic/. Just put it in the drawable-xhdpi folder and reference to it: `drawable.progress_horizontal_holo_light`. Enjoy. – AlikElzin-kilaka Jul 23 '13 at 05:46
  • Link no more aviable @AlikElzin-kilaka... – Joan Casadellà Jul 14 '16 at 16:05
1

It doesn't look like you are setting the ProgressBar's style attribute. From the docs for setIndeterminate():

If this progress bar's style only supports indeterminate mode (such as the circular progress bars), then this will be ignored.

You should manually set the style e.g. via style="@android:style/Widget.ProgressBar.Horizontal". Simply changing the Drawable isn't enough.

nmr
  • 16,625
  • 10
  • 53
  • 67
Geobits
  • 22,218
  • 6
  • 59
  • 103
0

Your default ProgressBar style has android:indeterminateOnly set to true. So you cant change ProgressBar's indeterminate state. source code.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
-1

Try also using setProgress(), which takes an integer argument between zero and getMax().

Karakuri
  • 38,365
  • 12
  • 84
  • 104