ProgressBar in Android: How to Create Your Own

progressbar androidAndroid is a popular programming language specifically designed for mobile phones and other touch screen devices. It is open source and implements component based architecture. This technology supports great graphics and is compatible with a wide range of hardware. What’s more it offers multiple layers of security along with optimization for low memory and low power usage. It is easy to customize the Android interface to any resolution. This makes it ideal to use in devices with different screen sizes. It’s the most popular mobile operating system today. In this beginner’s level tutorial we show how to create and work with progress bars using Android.

We assume that you have a working knowledge of the Java programming language. If not, you can take this course on Java essentials for Android to get a jump start.

What is Progress Bar?

At times the user needs to be notified that a certain task is still being done, and they need to wait. The controls which provide the information are usually called progress bars. Android has a class named ProgressBar which is instantiated to create progress bar objects.

Types of Progress Bars

There are two types of progress bars that you can use in your programs.

  1. Indeterminate Progress Bar: This is ideally used when the user either doesn’t know or does not want to display the task status. Here the progress is depicted by a continuously moving symbol which does not specify the amount of progress.
  2. Determinate Progress Bar: This progress bar can be either a horizontal bar or any other other shape. Determinate progress bar shows the quantitative amount of progress.

The default maximum value of the ProgressBar is 100. It is also possible to use the android:max attribute to set the desired maximum value.

In case the amount of work to be done is not known, indeterminate mode should be used. You can achieve this in two ways.

  • The first method is to use progressBar.setIndeterminate(true) in the activity class.
  • The other method is to use android:indeterminate=”true” in the layout xml file. In this mode the actual progress will not be displayed. A cyclic animation indicates that the task is in progress. The default symbol for the progress bar is a spinning wheel. In case, you want to display a horizontal progress bar you have to include the following attribute. The syntax is as follows:

You can learn more about ProgressBars and other notifications with this beginners course on Android.

ProgressBar vs  ProgressDialog

Progress Dialog box is quite similar to progress bar. However, it is a dialog box, not a bar; and the user has to wait for the task to complete, and take an action, before they can move on the next task. A Progress bar on the other hand, is a simple bar showing the progress so far. No user interaction is normally required.

How to Create a ProgressBar

You can create a progress bar by including the <ProgressBar> tag in the layout XML file. To set the maximum value of the progress bar you need to include the attribute android:max and assign a desired value to it. The attributes that you can use in the ProgressBar tag are the android:minWidth, android:minHeight and android:progress. The first two attributes are used to assign the desired width and height of the progress bar respectively. The third attribute determines the value by which the progress bar value is incremented.

If you want to show real time progress, the horizontal bar is ideal. The progress can be incremented by calling the following methods –  incrementProgressBy() or setProgress().

If you’re new to writing Android applications, you can refer to this course to get started.

Progress Bar Styles

  • Widget.ProgressBar.Small: This style shows a smaller version of the spinning wheel.
  • Widget.ProgressBar.Horizontal: This style is used if you want a horizontal bar.
  • Widget.ProgressBar.Large: This style shows a larger version of the spinning wheel.
  • Widget.ProgressBar.Inverse: Here the spinner has an inverse color scheme compared to the application color scheme.
  • Widget.ProgressBar.Small.Inverse: This style shows a smaller version of the spinning wheel with inverse color schemes.
  • Widget.ProgressBar.Large.Inverse: Use this style if you want a larger version of the spinning wheel with inverse color scheme.

Important Attributes of the ProgressBar Class

  • android:animationResolution: Use this attribute to specify time delay between frames of animation.
  • android:indeterminate: This has to be a boolean value. The only options are “true” or “false.” It is responsible to enable the indeterminate mode. Here if enabled the progress bar shows an infinite looping animation.
  • android:indeterminateBehavior: It determines how a indeterminate mode should behave when the maximum value of the progress bar is reached. If its value is 1 the progress starts from 0 onwards. If the value is 2 progress keeps the current value.
  • android:indeterminateDuration: This attribute is used to give the duration of the indeterminate animation as integer value.
  • android:progress: Use this attribute to set the default progress value which must be between 0 and maximum value.
  • android:secondaryProgress: A program can have two progress bars in operation. This attribute defines the secondary progress value which must be between 0 and maximum value.

Important Methods of the ProgressBar Class

  • ProgressBar(Context context: This method creates a new progress bar with initial progress value of zero and range between 0 and 100.
  • ProgressBar(Context context, AttributeSet attrs, int defStyle): This method creates a new progress bar with the option of setting its style.
  • getMax(): Use this method get the upper limit of the progress bar.
  • getProgress(): This method will let you know the current level of progress.
  • getSecondaryProgress(): Use this method to get the secondary progress value.
  • incrementProgressBy(int diff): Use this method to increase the progress by a specified value.
  • incrementSecondaryProgressBy(int diff): Increases the secondary progress by a specified value.
  • isIndeterminate(): This method is used to test whether the progress bar is in indeterminate mode.
  • setIndeterminate(boolean indeterminate): You can use this method to change the indeterminate mode.
  • setMax(int max): Use this method to define the upper limit of the progress bar.
  • setProgress(int progress): Use this method to set the current progress value.

We’ve covered just the basics of ProgressBars in this article. There’s much more to writing an Android application. You can take this course to get a holistic approach to becoming an Android developer. And while you’re at it, don’t forget to have fun!