android loading circle (spinner) between two activity

user5855887 picture user5855887 · Jan 29, 2016 · Viewed 34.2k times · Source

I have a main activity that has simple buttons and listeners associated to them. Each button opens a new activity (intent). However while opening activity, it takes some time to load which causes an UI freeze. I want to avoid this by simply adding a loading circle (spinner) in between. I've searched many posts but still couldn't find anything.

By loading circle I mean this

image

Answer

Parama Sudha picture Parama Sudha · Jan 29, 2016

That is a Progress Bar. You may create this programmatically or by using the widget in XML.

To implement in XML:

<ProgressBar
    android:id="@+id/progress_loader"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:visibility="visible" />

To implement in Java (using a Progress Dialog as you asked in comments, but you can also do this with the Progress Bar):

 ProgressDialog nDialog;
 nDialog = new ProgressDialog(Login.this);
 nDialog.setMessage("Loading..");
 nDialog.setTitle("Get Data");
 nDialog.setIndeterminate(false);
 nDialog.setCancelable(true);
 nDialog.show();

Before reaching a next activity, you should dismiss() the Progress Bar.

  nDialog.dismiss();