The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


Progress Indicators

Sometimes, we make the user wait. And wait. And wait some more.

Often, in these cases, it is useful to let the user know that something they requested is something that we are diligently working on. To do this, we can use some form of progress indicator. We saw basic use of a ProgressBar in the tutorials earlier in this book — now is the time to take a much closer look at ProgressBar and other means of displaying progress.

Prerequisites

Understanding this chapter requires that you have read the core chapters of this book. Having read the chapters on dialogs, custom drawables, and animators is also a good idea.

Progress Bars

The classic way to tell the user that we are doing something for them is to use a ProgressBar widget, much as we briefly displayed one in the EmPubLite sample app in the tutorials.

However, a ProgressBar is much more than a simple spinning image. We can use it to display either indeterminate progress (“we will be done… sometime”) or specific progress (“we are 34% complete”). We can use it either as a circle or as a classic horizontal bar, the latter typically used for specific progress. And, for specific progress, we can actually show two tiers of progress, known as “primary” and “secondary” (e.g., primary for the progress in copying a directory’s worth of files, secondary for the progress on a specific file).

In this section, we will take a look at these different ways of using ProgressBar.

Circular vs. Horizontal

As the name suggests, a ProgressBar denotes progress. As the name does not suggest, a ProgressBar is not a bar, by default — it is a circle. Hence, the following element from an XML layout resource:

  <ProgressBar
    android:id="@+id/progressCI"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"/>

gives us:

Android 5.1 ProgressBar, Default Style
Figure 628: Android 5.1 ProgressBar, Default Style

Android 4.0 ProgressBar, Default Style
Figure 629: Android 4.0 ProgressBar, Default Style

However, referencing style="?android:attr/progressBarStyleHorizontal" in the element:

  <ProgressBar
    android:id="@+id/progressHI"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:indeterminate="true"/>

gives us a horizontal bar:

Android 5.1 ProgressBar, Horizontal Style
Figure 630: Android 5.1 ProgressBar, Horizontal Style

Android 4.0 ProgressBar, Horizontal Style
Figure 631: Android 4.0 ProgressBar, Horizontal Style

Note that the look-and-feel of these widgets have changed over the years. On Android 1.x and 2.x, they will look like this:

Android 2.3.3 ProgressBar, Both Styles
Figure 632: Android 2.3.3 ProgressBar, Both Styles

Specific vs. Indeterminate

Typically, you use the circular ProgressBar style for indeterminate progress, where the circle simply spins in place to let the user know that work is proceeding and the device (or activity) has not frozen. The horizontal ProgressBar style is used to illustrate specific amounts of progress, from 0 to a value you choose.

However, while those patterns are typical, the choice of whether to use indeterminate or some specific amount of progress is independent of the style of the widget.

The android:indeterminate attribute controls whether the ProgressBar will render an indeterminate look or a specific look. For the latter, calls to setMax() (or the android:max attribute) will set the upper end of the progress range (the default is 100), and setProgress() or incrementProgressBy() will set how much progress along that range is illustrated.

Android 5.1 ProgressBar, Horizontal Style, Indeterminate and Specific
Figure 633: Android 5.1 ProgressBar, Horizontal Style, Indeterminate and Specific

Android 4.0 ProgressBar, Horizontal Style, Indeterminate and Specific
Figure 634: Android 4.0 ProgressBar, Horizontal Style, Indeterminate and Specific

Android 2.3.3 ProgressBar, Horizontal Style, Indeterminate and Specific
Figure 635: Android 2.3.3 ProgressBar, Horizontal Style, Indeterminate and Specific

Primary vs. Secondary

For specific progress, you actually have two independent amounts of progress. setProgress(), incrementProgressBy(), and android:progress control the primary progress, while setSecondaryProgress(), incrementSecondaryProgressBy(), and android:secondaryProgress control the secondary progress. Here, “primary progress” refers to the progress along an entire piece of work (e.g., copying a folder’s worth of files), while “secondary progress” refers the progress along a discrete chunk of the overall work (e.g., copying an individual file).

A ProgressBar will render these with different colors, though primary trumps secondary, and so the secondary progress will only be visible when its value exceeds that of the primary progress:

Android 4.0 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary
Figure 636: Android 4.0 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary

Android 2.3.3 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary
Figure 637: Android 2.3.3 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary

ProgressBar and Threads

The preview of this section is presently indisposed.

Tailoring Progress Bars

The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.

Progress Dialogs

The preview of this section may contain nuts.

Title Bar and Action Bar Progress Indicators

The preview of this section is in the process of being translated from its native Klingon.

Direct Progress Indication

The preview of this section was lost due to a rupture in the space-time continuum.