An indeterminate progress indicator on Android

Here’s the indeterminate progress indicator that we use in several apps during asynchronous operations. You may find it useful and we’ll refer to it in future posts.

The trivial code checks if an indicator is already on display and adds a timeout in case for whatever reason dismissal is never reached.

public void showProgress() {
    dismissProgress();
    progressDialog = new ProgressDialog(this);
    progressDialog.show();
    progressDialog.setCancelable(false);
    progressDialog.setContentView(R.layout.progress_dialog);
    progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));   
    timerDelayDismissProgress(15000, progressDialog);
}
 
public void dismissProgress() {
    if (null != progressDialog)
        progressDialog.dismiss();
}
 
public void timerDelayDismissProgress(long time, final ProgressDialog d) {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            d.dismiss();
        }
    }, time);
}

res/layout/progress_dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@drawable/progress_dialog_background"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="24dp" >
 
    <ProgressBar
        android:id="@+id/progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
 
</LinearLayout>

res/drawable/progress_dialog_background.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
 
    <solid android:color="#FF000000" />
 
    <stroke
        android:width="2dp"
        android:color="#ffffffff" />
 
    <corners android:radius="10dp" />
 
    <padding
        android:bottom="0dp"
        android:left="0dp"
        android:right="0dp"
        android:top="0dp" />
 
</shape>

device-2014-07-09-162824_framed

One thought on “An indeterminate progress indicator on Android”

Comments are closed.