A progress bar is a user interface control that shows how far an operation has progressed. Downloading a file, copying a file, or moving a file are all examples of operations. It’s similar to a graphical representation of an indicator that illustrates how a process or operation is progressing. Essentially, it shows how much of a task has been completed. In order to provide an interactive and user-friendly interface, a progress bar is essential.
Attributes Of Progress bar in Android
Attribute | Description |
---|---|
android:id | It is used to uniquely identify the control |
android:minHeight | It is used to set the height of the progress bar. |
android:minWidth | It is used to set the width of the progress bar. |
android:max | It is used to set the maximum value of the progress bar. |
android:progress | It is used to set the default progress value between 0 and max. It must be an integer value. |
It is used to set an acceleration curve for the indeterminate progress bars. | |
android: min | It defines the minimum value for the progress bar. |
android: progressTint | It applies Tint on progress indicator in the progress bar. |
android: indeterminate | It sets whether the progress bar is Determinate or Indeterminate. For this, there are two possible values that are True or False. |
android: animationResolution | It sets the timeout between frames of animation. Timeout is set in milliseconds. |
Methods of Progress Bar
There are certain methods of Android Progress Bar, out of which the most used and important methods are listed below:
- getMax()– It returns the maximum value that can be there in the progress bar.
- incrementProgressBy(int increment_value)– It increments the progress in the bar with the increment value that is passed in its parameter.
- setIndeterminate(boolean indeterminate)– It sets the progress bar to be either determinate or indeterminate. Passing ‘true’ means Indeterminate and passing ‘false’ means Determinate.
- setMax(int max_value)– It sets the maximum value of the progress in the progress bar.
- setProgress(int prog_val)– It updates the progress to the progress value that is passed in it.
- show(Context context, CharSequence title, CharSequence msg)– It displays the progress bar. It is a static method.
Types of Progress Bar in Android
These progress bars can be of different types like spinner wheel, determinate, and indeterminate. We will see these one by one-
Spinning Wheel Progress Bar
This one is android’s default progress bar. We define it by writing the following code:
<ProgressBar
android:id="@+id/p_Bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:progress="50"/>
Horizontal Progress Bar
We will define horizontal progress bar, and to define it we write the following code:
style="?android:attr/progressBarStyleHorizontal"
It is of two types that are:
i) Determinate Progress Bar
This progress bar is used when we know how long the operation will take place. In this, the actual progress of the operation is shown. For this, we set –
android:indeterminate="false"
And the actual definition would be as follows:
<ProgressBar
android:id="@+id/p_Bar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
android:progress="0" />
ii) Indeterminate Progress Bar
This progress bar is used when we do not know for how long the operation will take place. In this, the actual progress is not indicated but it indicates that the progress is taking place. For this, we set –
android:indeterminate="true"
And the actual definition would be as follows:
<ProgressBar
android:id="@+id/p_Bar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:max="100"
android:progress="0" />
Implementation of Android Progress Bar
Now we will move towards the implementation of the same. For this, we will follow the steps below:
1. First of all, we will create a new project and name it. I have named my project “ProgressBar”.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif"
android:text="Codingtimes"
android:textColor="#00574B"
android:textSize="45dp" />
<ProgressBar
android:id="@+id/p_Bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="150dp"
android:indeterminate="false"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="0" />
<ProgressBar
android:id="@+id/p_Bar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="400dp"
android:indeterminate="true"
android:max="100"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="0" />
<ProgressBar
android:id="@+id/p_Bar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="330dp"
android:minWidth="200dp"
android:minHeight="50dp"
android:progress="50" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/p_Bar"
android:layout_alignLeft="@+id/p_Bar" />
<TextView
android:id="@+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="100dp"
android:text="See Your Progress Here..." />
<Button
android:id="@+id/show_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_marginLeft="125dp"
android:layout_marginTop="20dp"
android:text="Start" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="460dp"
android:fontFamily="sans-serif"
android:text="Indeterminate Progress Bar" />
</RelativeLayout>
MainActivity.Java
package in.codingtimes.progressbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private ProgressBar pBar,pBar1,pBar2;
private int a = 0;
private TextView textView;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.tv);
pBar = findViewById(R.id.p_Bar);
Button button = findViewById(R.id.show_btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a = pBar.getProgress();
new Thread(new Runnable() {
@Override
public void run() {
while(a < 100){
a +=1;
handler.post(new Runnable() {
@Override
public void run() {
pBar.setProgress(a);
textView.setText(a + "/"+ pBar.getMax());
if(a == 100)
textView.setText("Your Progress has been Completed");
}
});
try {
Thread.sleep(50);
}catch (InterruptedException e){e.printStackTrace();
}
}
}
}).start();
}
});
}
}
That’s it!
You have successfully completed the post. Do Share : )
Peace Out!
Source Code – https://github.com/Adityaraj-30/ProgressBar_Android.git
Also Read – Android WebView with Example
Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy, KFC