public class

ProgressBar

extends View
java.lang.Object
   ↳ android.view.View
     ↳ android.widget.ProgressBar
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward. There is also a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.

A progress bar can also be made indeterminate. In indeterminate mode, the progress bar shows a cyclic animation. This mode is used by applications when the length of the task is unknown.

The following code example shows how a progress bar can be used from a worker thread to update the user interface to notify the user of progress:

 public class MyActivity extends Activity {
     private static final int PROGRESS = 0x1;

     private ProgressBar mProgress;
     private int mProgressStatus = 0;

     private Handler mHandler = new Handler();

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.progressbar_activity);

         mProgress = (ProgressBar) findViewById(R.id.progress_bar);

         // Start lengthy operation in a background thread
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     mProgressStatus = doWork();

                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();
     }
 }
 

XML attributes

See ProgressBar Attributes, View Attributes

Styles

Summary

XML Attributes
Attribute Name Related Method Description
android:progressBarStyle Default ProgressBar style. 
android:progressBarStyleHorizontal Horizontal ProgressBar style. 
android:progressBarStyleLarge Large ProgressBar style. 
android:progressBarStyleSmall Small ProgressBar style. 
[Expand]
Inherited XML Attributes
From class android.view.View
[Expand]
Inherited Constants
From class android.view.View
Public Constructors
ProgressBar(Context context)
Create a new progress bar with range 0...100 and initial progress of 0.
ProgressBar(Context context, AttributeSet attrs)
ProgressBar(Context context, AttributeSet attrs, int defStyle)
Public Methods
Drawable getIndeterminateDrawable()

Get the drawable used to draw the progress bar in indeterminate mode.

Interpolator getInterpolator()
Gets the acceleration curve type for the indeterminate animation.
synchronized int getMax()

Return the upper limit of this progress bar's range.

synchronized int getProgress()

Get the progress bar's current level of progress.

Drawable getProgressDrawable()

Get the drawable used to draw the progress bar in progress mode.

synchronized int getSecondaryProgress()

Get the progress bar's current level of secondary progress.

synchronized final void incrementProgressBy(int diff)

Increase the progress bar's progress by the specified amount.

synchronized final void incrementSecondaryProgressBy(int diff)

Increase the progress bar's secondary progress by the specified amount.

void invalidateDrawable(Drawable dr)
Invalidates the specified Drawable.
synchronized boolean isIndeterminate()

Indicate whether this progress bar is in indeterminate mode.

void onRestoreInstanceState(Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState().
Parcelable onSaveInstanceState()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state.
void postInvalidate()
Cause an invalidate to happen on a subsequent cycle through the event loop.
synchronized void setIndeterminate(boolean indeterminate)

Change the indeterminate mode for this progress bar.

void setIndeterminateDrawable(Drawable d)

Define the drawable used to draw the progress bar in indeterminate mode.

void setInterpolator(Context context, int resID)
Sets the acceleration curve for the indeterminate animation.
void setInterpolator(Interpolator interpolator)
Sets the acceleration curve for the indeterminate animation.
synchronized void setMax(int max)

Set the range of the progress bar to 0...

synchronized void setProgress(int progress)

Set the current progress to the specified value.

void setProgressDrawable(Drawable d)

Define the drawable used to draw the progress bar in progress mode.

synchronized void setSecondaryProgress(int secondaryProgress)

Set the current secondary progress to the specified value.

void setVisibility(int v)
Set the enabled state of this view.
Protected Methods
void drawableStateChanged()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
synchronized void onDraw(Canvas canvas)
Implement this to do your drawing.
synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
boolean verifyDrawable(Drawable who)
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.
[Expand]
Inherited Methods
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback

XML Attributes

android:progressBarStyle

Default ProgressBar style. This is a medium circular progress bar.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol progressBarStyle.

Related Methods

android:progressBarStyleHorizontal

Horizontal ProgressBar style. This is a horizontal progress bar.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol progressBarStyleHorizontal.

Related Methods

android:progressBarStyleLarge

Large ProgressBar style. This is a large circular progress bar.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol progressBarStyleLarge.

Related Methods

android:progressBarStyleSmall

Small ProgressBar style. This is a small circular progress bar.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol progressBarStyleSmall.

Related Methods

Public Constructors

public ProgressBar (Context context)

Create a new progress bar with range 0...100 and initial progress of 0.

Parameters
context the application environment

public ProgressBar (Context context, AttributeSet attrs)

public ProgressBar (Context context, AttributeSet attrs, int defStyle)

Public Methods

public Drawable getIndeterminateDrawable ()

Get the drawable used to draw the progress bar in indeterminate mode.

Returns

public Interpolator getInterpolator ()

Gets the acceleration curve type for the indeterminate animation.

Returns

public synchronized int getMax ()

Return the upper limit of this progress bar's range.

Returns
  • a positive integer

public synchronized int getProgress ()

Get the progress bar's current level of progress. Return 0 when the progress bar is in indeterminate mode.

Returns
  • the current progress, between 0 and getMax()

public Drawable getProgressDrawable ()

Get the drawable used to draw the progress bar in progress mode.

Returns

public synchronized int getSecondaryProgress ()

Get the progress bar's current level of secondary progress. Return 0 when the progress bar is in indeterminate mode.

Returns
  • the current secondary progress, between 0 and getMax()

public final synchronized void incrementProgressBy (int diff)

Increase the progress bar's progress by the specified amount.

Parameters
diff the amount by which the progress must be increased
See Also

public final synchronized void incrementSecondaryProgressBy (int diff)

Increase the progress bar's secondary progress by the specified amount.

Parameters
diff the amount by which the secondary progress must be increased

public void invalidateDrawable (Drawable dr)

Invalidates the specified Drawable.

Parameters
dr the drawable to invalidate

public synchronized boolean isIndeterminate ()

Indicate whether this progress bar is in indeterminate mode.

Returns
  • true if the progress bar is in indeterminate mode

public void onRestoreInstanceState (Parcelable state)

Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState(). This function will never be called with a null state.

Parameters
state The frozen state that had previously been returned by onSaveInstanceState().

public Parcelable onSaveInstanceState ()

Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.

Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.

Returns
  • Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.

public void postInvalidate ()

Cause an invalidate to happen on a subsequent cycle through the event loop. Use this to invalidate the View from a non-UI thread.

public synchronized void setIndeterminate (boolean indeterminate)

Change the indeterminate mode for this progress bar. In indeterminate mode, the progress is ignored and the progress bar shows an infinite animation instead.

If this progress bar's style only supports indeterminate mode (such as the circular progress bars), then this will be ignored.

Parameters
indeterminate true to enable the indeterminate mode

public void setIndeterminateDrawable (Drawable d)

Define the drawable used to draw the progress bar in indeterminate mode.

Parameters
d the new drawable

public void setInterpolator (Context context, int resID)

Sets the acceleration curve for the indeterminate animation. The interpolator is loaded as a resource from the specified context.

Parameters
context The application environment
resID The resource identifier of the interpolator to load

public void setInterpolator (Interpolator interpolator)

Sets the acceleration curve for the indeterminate animation. Defaults to a linear interpolation.

Parameters
interpolator The interpolator which defines the acceleration curve

public synchronized void setMax (int max)

Set the range of the progress bar to 0...max.

Parameters
max the upper range of this progress bar

public synchronized void setProgress (int progress)

Set the current progress to the specified value. Does not do anything if the progress bar is in indeterminate mode.

Parameters
progress the new progress, between 0 and getMax()

public void setProgressDrawable (Drawable d)

Define the drawable used to draw the progress bar in progress mode.

Parameters
d the new drawable

public synchronized void setSecondaryProgress (int secondaryProgress)

Set the current secondary progress to the specified value. Does not do anything if the progress bar is in indeterminate mode.

Parameters
secondaryProgress the new secondary progress, between 0 and getMax()

public void setVisibility (int v)

Set the enabled state of this view.

Parameters
v One of VISIBLE, INVISIBLE, or GONE.

Protected Methods

protected void drawableStateChanged ()

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

Be sure to call through to the superclass when overriding this function.

protected synchronized void onDraw (Canvas canvas)

Implement this to do your drawing.

Parameters
canvas the canvas on which the background will be drawn

protected synchronized void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w Current width of this view.
h Current height of this view.
oldw Old width of this view.
oldh Old height of this view.

protected boolean verifyDrawable (Drawable who)

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

Be sure to call through to the super class when overriding this function.

Parameters
who The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
Returns
  • boolean If true than the Drawable is being displayed in the view; else false and it is not allowed to animate.