19

I have created a ProgressBar programmatically as below in my activity. How do I make it show?

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);
0xCursor
  • 2,242
  • 4
  • 15
  • 33
user6304758
  • 201
  • 1
  • 2
  • 3
  • Thanks for the replies. But I am not using a ProgressDialog. I want the progressBar to show within the activity as indeterminate. Thanks. – user6304758 Jul 02 '16 at 14:44

8 Answers8

13

You can try this code for adding progressBar programaticlly to your layout.

RelativeLayout layout = new RelativeLayout(this);
ProgressBar progressBar = new ProgressBar(YourActivity.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);
MosabJ
  • 80
  • 1
  • 9
motis10
  • 2,484
  • 1
  • 22
  • 46
1
ProgressBar prog = new ProgressBar(MainActivity.this);
linear1.addView(prog);

linear1 is a LinearLayout and MainActivity is your activity. Change linear1 and MainActivity according to your needs.

Pang
  • 9,564
  • 146
  • 81
  • 122
0

Every activity has a content view. This is the root view of your activity that you set by calling setContentView(). Every view on screen must be a child of this view (or a child of a child, etc). The exception to this is dialogs, which come up in a separate window, but that's another discussion.

If you want the view to appear on screen, you must add it to some ViewGroup inside of your content view.

Actually the normal way you use progress bars for loading is different. Normally you add one to your xml, but you set its visibility to GONE so it doesn't appear. Then when you want it to appear you set it to VISIBLE. So it looks like the progress bar appears, but its really been there hidden all along.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

you can add progressBar in xml file like this

<ProgressBar
            android:id="@+id/pbProgress"
            android:style="@style/Spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

and visible ,invisible it pragmatically .

This the way to added progress bar at your xml file but if you want to added pragmatically you can use progressBar.setVisibility(View.Visible) to show progress bar or you can use progressBar.setVisibility(View.Gone) to hide progress bar on activity.

Suchi
  • 116
  • 1
  • 11
  • 3
    I know this is an old question, but he really asked **Programmatically**, your answer discusses how to add it manually via XML, and you didn't even include how to make it visible and invisible programmatically. Please edit your answer – MosabJ Nov 22 '16 at 05:24
  • That's not a ideal solution of you are working with a view inside a RecyclerView. – Pedro Paulo Amorim Jan 31 '20 at 15:56
-1

progressBar is a widget (view). You need to add to viewgroup.

You can use ProgressDialog to show progress loading Example:

ProgressDialog proDialog = ProgressDialog.show(this, "title", "message");
Liem Vo
  • 5,149
  • 2
  • 18
  • 16
-1

you can use in layout:

<RelativeLayout
    android:id="@+id/rlLoading"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ad000000"
    android:visibility="gone">


    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="@dimen/toolbar_height_70dp"
        android:layout_height="@dimen/toolbar_height_70dp"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        app:lottie_autoPlay="true"
        app:lottie_fileName="lottie/circle-l.json"
        app:lottie_loop="true"

        />


</RelativeLayout>

and in java class:

rlLoading.setVisibility(View.VISIBLE); Utility.disableEnableControls(false,rlRoot);

 public static void disableEnableControls(boolean enable, ViewGroup vg) {
    for (int i = 0; i < vg.getChildCount(); i++) {
        View child = vg.getChildAt(i);
        child.setEnabled(enable);
        if (child instanceof ViewGroup) {
            disableEnableControls(enable, (ViewGroup) child);
        }
    }
}
-1

My way is wrapping it inside an AlertDialog, call the dialog and use show(), hide() and some related functions when you need it. So you don't need to set the ProgressBar in your main XML layout file and can call it without setting layout parameters programmatically.

In layout file layout_pb.xml

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">
    <ProgressBar
        android:id="@+id/mProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>
    <!--add other customize layout components you want-->
</LinearLayout>

In Java code

AlertDialog progressDialog = new AlertDialog.Builder(this)
                                 .setView(R.layout.layout_pb)
                                 .setPositiveButton("CANCEL", cancel listener ...)
                                 ...
                                 .show();
gevge
  • 399
  • 2
  • 6
-3

First u need import the package

import android.app.ProgressDialog;

then use this,

    ProgressDialog progressDialog;

    progressDialog = new ProgressDialog(this);

    progressDialog.setMessage("MESSAGE");
    progressDialog.show();
    ........
    //tour task
    ........
    progressDialog.dismiss(); //dismiss progress bar
premnath
  • 53
  • 6