Qing
Qing

Reputation: 3079

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);

Upvotes: 19

Views: 73227

Answers (7)

Suraj Vaishnav
Suraj Vaishnav

Reputation: 8305

You can easily do via scaleX and scaleY property, here's how:

progressBar.scaleX=0.5f
progressBar.scaleY=0.5f

Here,

scaleX: To change the width, so 0.5 will reduce the width to the half of the actual width

scaleY: To change the height, so 0.5 will reduce the height to the half of actual height

Upvotes: 4

Latief Anwar
Latief Anwar

Reputation: 1868

Single line solution, try this

progressBar.setPadding(0,24,0,24);//left,top,right,bottom

Upvotes: -1

KaeM
KaeM

Reputation: 335

The easies way is by scale:

public class CustomProgressBarRenderer :ProgressBarRenderer
{
  protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
  {
    base.OnElementChanged(e);

    Control.ProgressDrawable.SetColorFilter(Color.FromRgb(182, 231, 233).ToAndroid(), Android.Graphics.PorterDuff.Mode.SrcIn);
    Control.ProgressTintList = Android.Content.Res.ColorStateList.ValueOf(Color.FromRgb(182, 231, 233).ToAndroid());
    Control.ScaleY = 10; // This changes the height

  }
}

Upvotes: 3

bat-el.g
bat-el.g

Reputation: 309

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" />

Upvotes: 11

Asif Raza
Asif Raza

Reputation: 3695

Let's give it a try...

I have fix the size of progressBar with add some padding.

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="45dp"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/category_img"
            android:scaleType="fitXY"
            tools:ignore="ContentDescription" />
    </RelativeLayout>

Now Looks like this:

enter image description here

Upvotes: 3

bhuvesh
bhuvesh

Reputation: 749

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" />

Upvotes: 23

Peter
Peter

Reputation: 2500

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.

Upvotes: 29

Related Questions