How do I set an Android ProgressBar's Size programmatically?
You can use LayoutParams to change width and height to whatever you want.
ProgressBar progressBar = new ProgressBar(teste.this, null, android.R.attr.progressBarStyleHorizontal);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(300, 10);
progressBar.setLayoutParams(params );
LinearLayout test = new LinearLayout(getApplicationContext());
test.addView(progressBar);
setContentView(test);
Also when you add a view, you can use this code:
test.addView(progressBar,50,50);
where the first parameter is width and the second is height.
You can also set the size of your progressbar from within the xml, just use android:maxHeight
, android:minHeight
, android:minWidth
and android:maxWidth
properties inside your ProgressBar
tag.
for instance, this will set the height of the progressbar to 35dip
and width to 10dip
,
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="60dip"
android:maxHeight="35dip"
android:minHeight="35dip"
android:minWidth="10dip"
android:maxWidth="10dip"
android:visibility="visible" />
In the xml, set the style, for example,
style="?android:attr/progressBarStyleLarge"
<ProgressBar
android:id="@+id/progressBar3"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />