Android ProgressBar

Android progressbar can be defined as below –

"A progressBar is a view that represents progress of an operation. For example, if you upload or download anything from the internet, it's better to show the user the progress of downloading/uploading."

We can use ProgressBar in two ways depending on the types of operation. If you know the duration of an operation, you should use determinate progressBar. Determinate progressBar works only for a limited period of time. Or, if you don't know the duration of an operation, you should use indeterminate ProgressBar. Indeterminate progressBar works until you stop it.

 

 

 

 

Now, we will go through different attributes of progressbar that can be used to customise this prorgressbar.

S. No. XML Attributes Description
1 android:id This is unique id of the progressbar to uniquely identify the progressbar.
2 android:elevation Specifies base z-depth of the view.
3 android:clickable Specifies if this view is clickable or not.
4 android:background Sets background drawable for the view.
5 android:onClick Specifies action to perform when this view is clicked.
6 android:padding Specifies padding of the view.
7 android:visibility Specifies visibility of the view. For example, VISIBLE, INVISIBLE etc.
8 android:alpha Sets alpha of view.
9 android:indeterminate Enables indeterminate mode of the progressbar.
10 android:indeterminateDrawable Sets drawable of the progressbar for the indeterminate mode.
11 android:indeterminateDuration Sets duration of the indeterminate animation.
12 android:max Sets maximum value.
13 android:min Sets minimum value.
14 android:progress Defines default progress value (between 0 and max value).
15 android:progressDrawable Drawable used for the progress mode.
16 android:secondaryProgress Defines secondary progress value (between 0 and max value).

 

 

 

 

At first, we will create android project. Then, we'll use progressBar widget in this project.

Follow the steps below to create new project. Please ignore the steps if you've already created a new project.

S. No. Steps
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as ProgressBar. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
  If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use progressBar in android.

Open res/values/strings.xml file. Then, add below code into it.

<resources>
<string name="app_name">ProgressBar</string>
</resources> 

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
 <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"
 tools:context=".MainActivity">
 
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide progressbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@id/progress_circular" />
 
</android.support.constraint.ConstraintLayout>

In activity_main.xml file, we have added progressbar and button in xml file. Now, we will access this progressbar in MainActivity.java file. Then, we will show/hide progressbar on button click.

 

 

 

 

Open src/main/java/com.ukacademe.progressbar/MainActivity.java file. Then, add below code into it.

package com.ukacademe.progressbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar progressBar = findViewById(R.id.progress_circular);
        if (progressBar != null) {
            final Button btnShow = findViewById(R.id.btnShow);
            if (btnShow != null) {
                btnShow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int visibility = (progressBar.getVisibility() == View.GONE) ? View.VISIBLE : View.GONE;
                        progressBar.setVisibility(visibility);

                        String btnText = (progressBar.getVisibility() == View.GONE) ? "SHOW PROGRESSBAR" : "HIDE PROGRESSBAR";
                        btnShow.setText(btnText);
                    }
                });
            }

        }
    }
}

In MainActivity.java, we have accessed progressbar and button defined in activity_main.xml file. Then, we have added click listener on button to show/hide progressbar.

Since AndroidManifest.xml file is very important in any android application, we will also go through code inside this file.

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ukacademe.progressbar">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

When we run the application, we will get output as shown below.

UK Academe-ProgressBar-Output