Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering ProgressBar Programmatically in Android

I'm trying to center a ProgressBar programmatically using the following:

ViewGroup layout = (ViewGroup) findViewById(android.R.id.content).getRootView();
progressBar = newProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

The size setting seems to work okay, but the ProgressBar doesn't center in the existing layout (defined by xml with a relative layout). Is there something obviously wrong here?

The XML is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".test"
android:typeface="monospace">

</RelativeLayout>

i.e. it's just an empty relative layout to test with and see if I can get it to programmatically add a progress bar.

Thanks.

like image 962
James B Avatar asked Aug 18 '13 18:08

James B


2 Answers

I wrote a class based on @DharmarajRupakheti's answer:

public class ProgressBarHandler {
    private ProgressBar mProgressBar;
    private Context mContext;

    public ProgressBarHandler(Context context) {
        mContext = context;

        ViewGroup layout = (ViewGroup) ((Activity) context).findViewById(android.R.id.content).getRootView();

        mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
        mProgressBar.setIndeterminate(true);

        RelativeLayout.LayoutParams params = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

        RelativeLayout rl = new RelativeLayout(context);

        rl.setGravity(Gravity.CENTER);
        rl.addView(mProgressBar);

        layout.addView(rl, params);

        hide();
    }

    public void show() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    public void hide() {
        mProgressBar.setVisibility(View.INVISIBLE);
    }
}

Usage:

mProgressBarHandler = new ProgressBarHandler(this); // In onCreate
mProgressBarHandler.show(); // To show the progress bar
mProgressBarHandler.hide(); // To hide the progress bar
like image 72
nima Avatar answered Sep 25 '22 20:09

nima


If you want to do it programatically you can do it like below:

RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(SignInActivity.this,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);

setContentView(layout);
like image 42
tasomaniac Avatar answered Sep 25 '22 20:09

tasomaniac