Reputation: 256
I want to show a progress Circle in my app while loading data. I have an activity and moving from one activity to another I am parsing some xml data so for the time being until the parsing is completed I want to show a circular loading effect.
Upvotes: 19
Views: 51651
Reputation: 327
You need to create animation xml file in res/anim folder and call startAnimation in your ImageView when you are loading data and stopAnimation when you stop loading data. And set loading image to ImageView, for example:
This id code for circle animation xml file
<?xml version="1.0" encoding="UTF-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:duration="1200"
android:interpolator="@android:anim/linear_interpolator" />
Upvotes: 17
Reputation: 2470
Insert this beetween two View tags
<ProgressBar android:id="@+id/loading_spinner"
style="?android:progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
Upvotes: 4
Reputation: 143
To expand a bit on trgraglia's answer, you could create an indeterminate (behavior: cycle) progress bar and set the visibility to false. During AsyncTask preExecute(), make it visible, and during AsyncTask onPostExecute(), turn it back to invisible (provided you are staying on the same activity or w/e case you may need). This eliminates the need to create an animation.
Upvotes: 1
Reputation: 39884
You can use an indeterminate ProgressBar for the circular loading effect. Here is how you do it in XML:
<ProgressBar android:indeterminate="true"
android:layout_width="50dp" android:layout_height="50dp"
android:id="@+id/marker_progress" style="?android:attr/progressBarStyle"
android:layout_gravity="center_vertical|center_horizontal"/>
You can change the height and width to be what you want. When you are done loading, you can change it's visibility to View.INVISIBLE or View.GONE
Upvotes: 29
Reputation: 17557
Use AsyncTask for loading and parsing:
/**
* Background task that fetched the content from server and parses the content.
*/
private class BackgroundLoadingTask extends AsyncTask<InputStream, Void, Boolean> {
@Override
protected void onPreExecute() {
// show the progress bar
Activity.this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
Activity.this.requestWindowFeature(Window.PROGRESS_VISIBILITY_ON);
}
@Override
protected Boolean doInBackground(InputStream... params) {
// try to load XML from HTTP server and parse
return true;
}
@Override
protected void onPostExecute(Boolean parsingError) {
// hide the progress bar
Activity.this.requestWindowFeature(Window.PROGRESS_VISIBILITY_OFF);
}
}
Upvotes: 2
Reputation: 5435
Just as you would the progress dialog, use an animation instead with async task and just make it visible on prexecution and hide it again on post.
Upvotes: 1
Reputation: 1909
Why not use the progressbar UI
myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);
dan
Upvotes: 3