Android Indeterminate Horizontal Progress Bar Programmatically

We will Programmatically create Android Indeterminate Horizontal Progress Bar example tutorial today.

Generally, we use progress bar when we want to tell user that some process going on.

Here, user have to wait until the process is completed. The best way to handle this situation is to animate some progress symbol.

Now process also can be of two types. One which have specific time amount to complete it’s operation which is called determinate.

Other one is indeterminate where you do not have any clue about the total time required by the process.

In determinate process, you can show percentage to give user a little idea about how long he needs to wait.

While in the case of indeterminate you can not show any indicator like percentage.

So you need to create a specific indeterminate progress bar for scenario.

We will create horizontal indeterminate progress bar in this example.

This example includes four different types of indeterminate progress bars.

Follow all the below steps to complete this tutorial.

Various Progress bar Tutorials

Step 1. Main Layout

First of all, create a new activity in the android studio. It is advisable to select “Empty Activity” while creating fresh project in the android studio.

We will define our xml code for progress bars in the activity_main.xml file.

Add the below code snippet in the activity_main.xml file.

First three progress bars have android’s in built style for horizontal progress bar.

Below is the style for first progress bar.

Below two styles are for second and third progress bars respectively.

Now consider the below coding lines for fourth progress bar.

  • Here, we will use custom drawable along with the in-built style.
  • Drawable file will give specific properties to make custom horizontal progress bar.
  • We have used second.xml drawable file in the fourth progress bar.

Let us create second.xml file.

Create a new drawable xml file under directory res->drawable. Name it as second.xml

Copy the following code in it.

Step 2. Main Code

After working on XML files, it is time to play with java file.

Copy the following code snippet in MainActivity.java file.

Because we are using just android’s in-built style in the first three progress bars, we do not need to add any coding lines in java file for these progress bars.

We have handled some scenarios for the fourth one.

Consider the below code

  • First line is declaring the object of the ProgressBar class.
  • Second one is creating the integer variable “pStatus” with zero as the initial value.
  • Third line is giving us Handler’s object.

Let us understand the below code structure for fourth progress bar.

  •  First, we will initialize handler object to avoid null pointer exception.
  • Then we will create a new thread to start the horizontal progress bar.
  • Compiler will check for while() method in the run() method of the thread.
  • In while() loop, compiler will check if the value of the integer variable pStatus is less than 100 or not.
  • If pstatus has less value than 100 then compiler will enter in the while loop. After entering, it will increase the value of pStatus by one.
  • Now it will again check one if condition. If the value of pStatus is 99 than it will set the value of pStatus as zero.
  • This if condition is responsible for the infinite running of the thread hence the infinite running of the horizontal progress bar.
  • After this, compiler will set the value of the primary progress and the secondary progress.

All Four Horizontal Progress Bars

Below is the visual representation of all four progress bars.

via GIPHY

Download Source Code For Android Indeterminate Horizontal Progress Bar Example

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *