16

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.

James B
  • 8,975
  • 13
  • 45
  • 83
  • Please post the XML :) – An SO User Aug 18 '13 at 18:12
  • 1
    Why do you take effort to create programmatically, when you can achieve it in xml? – VenomVendor Aug 18 '13 at 18:13
  • I'm creating it as a class as it's a view I want to inject dynamically as and when different processes are taking place in the application. Doing it this way means I don't have to put in all the layouts. I can just instantiate it as an object and append it to the current layout. – James B Aug 18 '13 at 18:20

4 Answers4

36

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
Community
  • 1
  • 1
nima
  • 6,566
  • 4
  • 45
  • 57
  • 1
    This should be the correct way of doing it considering the fact that progressBar in Toolbar is deprecated – Daniel May 08 '15 at 10:35
  • Thanks for answer, great way to implement ProgressBar dynamically – Rajan Maurya Jun 19 '16 at 08:44
  • 1
    I believe this will leak the Context object. You'll want to make it a WeakReference - or, since you're not even using it outside of the first method, just don't store a reference to it at all. – kpsharp Sep 05 '17 at 20:50
  • Hi nima,is this solution is a tested one?Will it throw any exception? – kgandroid Feb 11 '19 at 07:53
  • Yes, I used it in my apps, but it was 5 years ago and maybe it needs to be updated. – nima Feb 13 '19 at 07:13
  • I have been trying to show progress bar via alter dialog but i am having some issues can you please check my [question](https://stackoverflow.com/q/63393532/8868582)? – Faisal Qayyum Aug 13 '20 at 11:04
31

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);
Davor Zlotrg
  • 6,020
  • 2
  • 33
  • 51
tasomaniac
  • 10,234
  • 6
  • 52
  • 84
  • Thank you! I'm slowly starting to understand this Android business now. – James B Aug 18 '13 at 18:27
  • Actually the problem with your implemantation is that you use RelativeLayout parameters in your ProgressBar but the outer parent layout is not a RelativeLayout at all. You should put the progressbar in a relative layout in order CENTER_IN_PARENT to work. Otherwise it will just ignored. – tasomaniac Aug 18 '13 at 18:29
  • That works well tasomaniac for a new layout, but what if I want to append the progress bar to an existing layout defined in xml. The above only shows the progress bar i.e., it's a new layout. thanx. – James B Aug 18 '13 at 18:32
  • If I were you, I wouldn't use it programatically. Add the progressbar to your layout and use android:visibility="gone" and when you want to show it, use setVisibility(View.VISIBLE) programatically. – tasomaniac Aug 18 '13 at 18:35
  • That's what I had been doing up to now, but quite liked the idea of not having to put it into all the layouts (not that it's a particularly big deal). Quick question: you said the outer parent is not in relative layout? I'm confused, as isn't defined as such in the xml file (see original question post). – James B Aug 18 '13 at 18:38
  • No, you have a RelativeLayout indeed but you are adding the progressBar not in that RelativeLayout. You are adding it to the main content view. You can give an id to that RelativeLayout. And then get it by calling findViewById() and then add the progressBar to it. – tasomaniac Aug 18 '13 at 19:19
  • I have been trying to show progress bar via alter dialog but i am having some issues can you please check my [question](https://stackoverflow.com/q/63393532/8868582)? – Faisal Qayyum Aug 13 '20 at 11:03
  • I have been trying to show progress bar via alter dialog but i am having some issues can you please check my [question](https://stackoverflow.com/q/63393532/8868582)? – Faisal Qayyum Aug 13 '20 at 11:04
10

Use your coding in this style, this gives the result you want.

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

progressBar = new ProgressBar(thisActivity,null,android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);

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

RelativeLayout rl = new RelativeLayout(thisActivity);

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

layout.addView(rl,params);
nathanchere
  • 8,008
  • 15
  • 65
  • 86
Raj
  • 111
  • 1
  • 5
  • I have been trying to show progress bar via alter dialog but i am having some issues can you please check my [question](https://stackoverflow.com/q/63393532/8868582)? – Faisal Qayyum Aug 13 '20 at 11:04
-1

You don't need the XML. You can create the view all by code:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        ProgressBar pb = new ProgressBar(this, null,
                                         android.R.attr.progressBarStyleLarge);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        pb.setLayoutParams(params);
        layout.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        layout.addView(pb);
        setContentView(layout);
}
mabg
  • 1,894
  • 21
  • 28