Skip to content Skip to sidebar Skip to footer

Change The Width Of A Progressbar Added At Runtime

I am building up a UI from code and can successfully add ProgressBar widgets, however I cannot alter the dimensions of the widget to values I need, it always stays at the default s

Solution 1:

R.layout.activity includes this:

<ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:visibility="visible"
        style="@android:style/Widget.ProgressBar.Large.Inverse"
/>

And then the code:

publicvoidonCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    ProgressBarbar= (ProgressBar)findViewById(R.id.progress);
    bar.getLayoutParams().height = 500;
    bar.invalidate();
}

The call to the invalidate() method is important as that is when the view is redrawn to take the changes into account. Although unintuitive, it does allow for better performance by batching changes.

Solution 2:

There are minWidth/minHeight/maxWidth/maxHeight parameters as well, but (I think) they can only be set through XML or via a style/theme.

Otherwise, you could inflate your ProgressBar view from XML.

Solution 3:

So, you need to define your own style for the progress bar:

<stylename="SmallProgressBar"parent="android:Widget.ProgressBar"><itemname="android:minHeight">10dip</item><itemname="android:maxHeight">35dip</item></style>

Here, you can set min and max height and width of the progress bar and other cool features.

Now, you set in your xml layout style:

<ProgressBar
    android:id="@+id/custom_edit_text_progress_bar"
    style="@style/SmallProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:visibility="visible"/>

When you inflate your progress bar, you

getLayoutParams().height = 25

and

getLayoutParams().width = 25

for progress bar and set height and width.

NOTE: You cannot exceed height and width that you defined in styles.

Post a Comment for "Change The Width Of A Progressbar Added At Runtime"