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);
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);
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);
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.
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.
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.
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");
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);
}
}
}
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();
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