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.
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.
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.
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.
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
As the name suggests, a
ProgressBar denotes progress. As the name does not
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"/>
Figure 628: Android 5.1 ProgressBar, Default Style
Figure 629: Android 4.0 ProgressBar, Default Style
<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:
Figure 630: Android 5.1 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:
Figure 632: Android 2.3.3 ProgressBar, Both Styles
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.
android:indeterminate attribute controls whether the
render an indeterminate look or a specific look. For the latter, calls
setMax() (or the
android:max attribute) will set the upper end of the
progress range (the default is 100),
incrementProgressBy() will set how much progress
along that range is illustrated.
Figure 633: Android 5.1 ProgressBar, Horizontal Style, Indeterminate and Specific
Figure 634: Android 4.0 ProgressBar, Horizontal Style, Indeterminate and Specific
Figure 635: Android 2.3.3 ProgressBar, Horizontal Style, Indeterminate and Specific
For specific progress, you actually have two independent amounts of progress.
android:progress control the
primary progress, while
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).
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:
Figure 636: Android 4.0 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary
Figure 637: Android 2.3.3 ProgressBar, Horizontal Style, Primary-Only and Primary-Plus-Secondary
The preview of this section is presently indisposed.
The preview of this section was accidentally identified as an Android 'tasty treat' by the Cookie Monster.
The preview of this section may contain nuts.
The preview of this section is in the process of being translated from its native Klingon.
The preview of this section was lost due to a rupture in the space-time continuum.