ProgressBar

ProgressBar is a user interface element that indicates the progress of an operation. Progress bar supports two modes to represent progress: determinate, and indeterminate. We should display progress bars to a user in a non-interruptive way in the app’s user interface or in a notification instead of within a dialog. ProgressDialog has been deprecated in API level 26.

Indeterminate Progress

Use indeterminate mode for the progress bar when you do not know how long an operation will take. Indeterminate mode is the default for progress bar and shows a cyclic animation without a specific amount of progress indicated. The following example shows an indeterminate progress bar:

<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

Determinate Progress

Use determinate mode for the progress bar when you want to show that a specific quantity of progress has occurred. For example, the percentage of an audio file played by MediaPlayer.

To indicate determinate progress, set the style of the progress bar to R.style.Widget_ProgressBar_Horizontal and set the amount of progress. The following example shows a determinate progress bar that is 25% complete:

<ProgressBar
android:id="@+id/determinateBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="25"/>

You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100. You can adjust this default by setting the android:max attribute.

Other progress bar styles provided by the system include:

• Widget.ProgressBar.Horizontal
• Widget.ProgressBar.Small
• Widget.ProgressBar.Large
• Widget.ProgressBar.Inverse
• Widget.ProgressBar.Small.Inverse
• Widget.ProgressBar.Large.Inverse

The “inverse” styles provide an inverse color scheme for the spinner, which may be necessary if your application uses a light colored theme (a white background).

Few Public Methods which can be used for ProgressBar

Methods returning int value:

getMax()
Return the upper limit of this progress bar’s range in int form.

getMin()
Return the lower limit of this progress bar’s range in int form.

getProgress()
Get the progress bar’s current level of progress in int form.

void methods:

incrementProgressBy(int diff)
Increase the progress bar’s progress by the specified amount.

setIndeterminateDrawable(Drawable d)
Define the drawable used to draw the progress bar in indeterminate mode.

setMax(int max)
Set the upper range of the progress bar max.

setMin(int min)
Set the lower range of the progress bar to min.

setProgress(int progress)
Sets the current progress to the specified value.

setProgress(int progress, boolean animate)
Sets the current progress to the specified value, optionally animating the visual position between the current and target values.

setProgressDrawable(Drawable d)
Define the drawable used to draw the progress bar in progress mode.

Leave a Reply