19

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.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
ta54
  • 256
  • 1
  • 2
  • 8
  • 2
    This is how I did it http://stackoverflow.com/questions/19655715/progress-dialog-is-closed-when-touch-on-screen/22939109#22939109 – Ali Apr 29 '14 at 09:45

7 Answers7

29

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

Abhinav
  • 38,516
  • 9
  • 41
  • 49
17

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:

loading

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" />
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
RomaTTi
  • 327
  • 2
  • 12
  • I am using a List view with custom adapter. and i want to show the loading effect in that list view. how i can get that parsing is done and we can stop this animation – ta54 Mar 10 '11 at 13:36
  • I am using List view too. And when another thread parsing xml, i set to lost view special loadingAdapter wich return only one view. This view is imageView that fill all parent and fit image to center. When parsing thread stop parse i set to lest view my another custom Adapter wich provides UI elements. – RomaTTi Mar 10 '11 at 13:47
  • Can you give me a example use it. Thanks! – Thang BA Mar 16 '16 at 04:03
4

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" />
Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36
3

Why not use the progressbar UI

myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);

dan

papachan
  • 1,891
  • 17
  • 20
2

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);


    }
  }
peceps
  • 17,370
  • 11
  • 72
  • 79
1

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.

Brandon Ward
  • 143
  • 3
  • 7
1

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.

Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75