Qing Qing - 4 months ago 55
Android Question

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

Answer

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.