Horizontal Progress Bar Code Snippet for Android Studio

 

Welcome. In this tutorial you will learn how to add a Horizontal Progress bar in Android Studio.

Android Horizontal Progress Bar Example

A progress bar is a bar that show you the progress of currently going operation. In android you can add your own progress bar, that too with material Design for a better look.

Progress bar is generally of two types, one is finite and the other one is infinite. The finite or determinate progress bar is generally horizontal and shows the progress as a percentage out of 100. It is used when the progress is initially known and you want to display a specific quantity of progress occurred. On the other hand, an infinite progress bar is somewhat circular in shape and it is used when the progress is unknown, or you cannot specifically say how much progress is being made.You have often seen a round bar rotating, which is generally a type of indeterminate progress bar.

Placing a horizontal progress bar in your app

You can place a horizontal progress bar by using the <ProgressBar> tag in your XML (activity_main.xml) file. The corresponding code is shown below:-

Here the progress is Initially set to 25% by using the android:progress attribute.

Note that the style=”?android:attr/progressBarStyleHorizontal” attribute is used to separate a determinate progress bar from an indeterminate progress bar. So if you want to add an Indeterminate or Circular Progress Bar in your source code, then just remove that line and you will get a Circular Progress Bar. A short example is shown below.

 

Updating Progress in a Horizontal Progress Bar

You can update the progress value of the bar from your java file by first creating a progressBar variable as shown.

Then within some function where you want to change the progress, just use

Here in this case we have set our progress to 80%. You can change it according to your need.

There are also other useful attributes of a progress bar like getProgress() to get the progressbar’s current level of progress. You can even use the setMax() property to set the upper level of the Progress Bar. Similarly you can also use setMin() for setting minimum value.

Note that you can also add a progress Bar from your java file too. The source code for that process is shown at the end.

 

A progressbar for example purpose is shown below where once the Download button is pressed, our progress will be shown.

horizontal progress bar snippet code example for Android Studio horizontal progress bar example for Android Studio

 

The Full Java Code along with the XML layout for creating the above progress bar is shown below:-

JAVA (MainActivity.java)

 

XML (activity_main.xml)

 

 

You can directly copy paste these snippets to your android studio, and they should work perfectly. Just save and run your project. You can also try to play with other progress bar settings on your own and see the corresponding changes accordingly. There are a variety of properties available which can be changed to make your Horizontal Progress Bar more materialized and give it a modern fancy look.

For a more detailed guide, you can also visit android’s official guide below:-

https://developer.android.com/reference/android/widget/ProgressBar

Comment down below if you are facing any problems.

 

Also Read:-

Leave a Reply

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