Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Android ProgressBar determinate in code? [duplicate]

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?

like image 527
AlikElzin-kilaka Avatar asked Jul 22 '13 15:07

AlikElzin-kilaka


2 Answers

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...

like image 54
ChD Computers Avatar answered Sep 20 '22 04:09

ChD Computers


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.

like image 33
Geobits Avatar answered Sep 22 '22 04:09

Geobits