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:-
1 2 3 4 5 6 7 |
<ProgressBar android:id="@+id/progressBar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:progress="25" </android.support.constraint.ConstraintLayout> |
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.
1 2 3 4 5 |
<ProgressBar android:id="@+id/circularBar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
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.
1 |
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar); |
Then within some function where you want to change the progress, just use
1 |
progressBar.setProgress(80); |
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.
The Full Java Code along with the XML layout for creating the above progress bar is shown below:-
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; public class Progressbar_Horizontal extends Activity { Button btnStartProgress; ProgressDialog progressBar; private int progressBarStatus = 0; private Handler progressBarHandler = new Handler(); private long fileSize = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progressbar__horizontal); addListenerOnButton(); } public void addListenerOnButton() { btnStartProgress = (Button) findViewById(R.id.btnStartProgress); btnStartProgress.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(true); progressBar.setMessage("File downloading ..."); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); progressBarStatus = 0; fileSize = 0; new Thread(new Runnable() { public void run() { while (progressBarStatus < 100) { progressBarStatus = doSomeTasks(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBarHandler.post(new Runnable() { public void run() { progressBar.setProgress(progressBarStatus); } }); } if (progressBarStatus >= 100) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.dismiss(); } } }).start(); } }); } public int doSomeTasks() { while (fileSize <= 1000000) { fileSize++; if (fileSize == 100000) { return 10; } else if (fileSize == 200000) { return 20; } else if (fileSize == 300000) { return 30; } } return 100; } } |
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F2F2F2" tools:context=".Progressbar_Horizontal"> <Button android:id="@+id/btnStartProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Download File" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
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.