Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an Android ProgressBar's Size programmatically?

I have a custom view that extends ViewGroup. It includes a ProgressBar and a WebView. I'm displaying the ProgressBar while the WebView is loading.

This works, but the ProgressBar is too big. How do I make it smaller?

Below is my non-working code:

webView = new WebView(context);
webView.setWebViewClient(new MyWebChromeClient());
webView.loadUrl("file://" + path);
addView(webView);

progressBar = new ProgressBar(mContext, null,
                                         android.R.attr.progressBarStyleSmall);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                                                    LayoutParams.WRAP_CONTENT);
progressBar.setLayoutParams(params);

addView(progressBar);
like image 390
Qing Avatar asked Mar 31 '11 08:03

Qing


People also ask

How do I change the progress bar icon on Android?

Go to the activity_main. xml file and refer to the following code. Open the activity_main. xml file and in the ProgressBar tag and set the drawable in indeterminateDrawable attribute.

How do I use ContentLoadingProgressBar?

ContentLoadingProgressBar implements a ProgressBar that waits a minimum time to be dismissed before showing. Once visible, the progress bar will be visible for a minimum amount of time to avoid "flashes" in the UI when an event could take a largely variable time to complete (from none, to a user perceivable amount).

How do I stop progress bar?

You want to stop the progressDialog or change its UI not to be circular? You can set your progressbar's visibility to invisible or gone through progressbar. setVisibility(View. INVISIBLE); or progressBar.


2 Answers

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.

like image 137
Peter Avatar answered Oct 14 '22 14:10

Peter


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" />
like image 32
bhuvesh Avatar answered Oct 14 '22 12:10

bhuvesh