Android Horizontal Progress Bar With Percentage Programmatically

Learn how to create Android Horizontal Progress Bar With Percentage Programmatically.

We will develop horizontal progress bar with percentage in this example tutorial.

There are many processes like downloading, uploading, fetching data from remote server etc. where you need to tell the user to wait for some time.

To accomplish this purpose, showing horizontal progress bar to the user is great option.

If you show the progress bar with percentage then the user will feel more comfortable while waiting.

Determinate Horizontal Progress bar

Determinate progress bar means that you tell user that how much process is completed in terms of percentage.

When you know the exact or approximate time amount which will be taken by the process, you should use determinate progress bar.

You should use determinate progress bar when you do not have any idea about timing of the process.

Other Progress Bar Example

We will create three horizontal progress bar with different designs in this tutorial.

Step 1. Drawable XML files

We need to create drawable xml files to develop various styles for progress bars.

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

Add below source code in it

Prepare another xml file under same directory.

Give name to that file as progress.xml

Write down following code in it

  • Above two examples will create custom style for our progress bars.
  • It will create parameters like shape, color, gradient effect etc.

Step 2. Launching Progress bar

Now we need to change activity_main.xml and MainActivity.java files.

Write down the below source code in activity_main.xml file

  • We are going to show three progress bars. So, I have taken three textviews, three buttons and three progress bars in this file.
  • All three textviews will show the completion percentage.
  • I have implemented button for each progress bar. When the user clicks on button, a progress bar related to that button will start it’s operations.

Now consider the below code

  • Above is our first progress bar.
  • I have used android’s in-built style for this progress bar. So we do not require to attach any drawable files in the above code.

Below is the code for second horizontal progress bar.

  • I have attached a drawable xml file progress_limit.xml to this progress bar.
  • This progress bar contains two types of progresses. Primary progress and Secondary Progress.
  • Both progresses will increase it’s value simultaneously with the loading of the process.
  • In this code, I have set the secondary progress as 100 means that it will have full value.

Following is the third progress bar source code.

  • I have attached progress.xml file from drawable directory which we developed in Step 1.
  • We will show different colors in the progress bars with help of xml files of drawable directory.

Finally, write down the following coding lines in MainActivity.java

Look at the below code

  • I have declared three integer variables. These variables will represent the amount or percentage of the completed process.

Following code will run the progress bar.

  • I have used handler class to start the thread. This thread will approximately take 3 seconds complete.
  • It means that our progress bar will start from 0 and will reach at 100 percentage in 3 seconds.
  • You change the time as per your requirements using the following line.

  • Change the amount to other than 20 in the above code.
  • In the thread, compiler will check one while() loop.
  • if the value of the progressStatus variable is less than 100 then compiler will execute the code inside the while() loop, otherwise, it will terminate the thread.
  • When progressStatus is less than 100 then the compiler will increase the value of progressStatus by one unit.

After that, it will update the value of textview with latest value of progressStatus using below line.

  • Above code will also set the progressStatus to the progress bar.

View Output

This video demonstrate how our example’s output will look a like.

Download Source Code For Android 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 *