AsfK AsfK - 11 months ago 70
Android Question

Update Horizontal Progress Bar width programmatically (In horizontal scrollview)

I have a progress bar in scroll view and I would like to modify the width programmatically, but it's looks like it's impossible, in the xml I have only Progress bar like this:

<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
</HorizontalScrollView>


And the code is very simple too:

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMinimumWidth(3000);
progressBar.invalidate(); // tried called invalidate although setMinimumWidth called requestLayout, not help


I also tried to set layout width, like this:

progressBar.getLayoutParams().width = 3000;
progressBar.invalidate();


But found it's works only for progressbar that not in scroll view...

Any idea how can I update progress bar width (in horizontal scroll view)? and why the scroll view defect the progress bar width?
Thanks!

Answer Source

You aren't setting the params the right way. Try this:

HorizontalScrollView.LayoutParams params = (HorizontalScrollView.LayoutParams) progressBar.getLayoutParams();
params.width = 3000;
progressBar.setLayoutParams(params);