Tutorial

Android ProgressBar Example

Published on August 3, 2022
author

Anupam Chugh

Android ProgressBar Example

Welcome to Android ProgressBar Example. Today we’ll implement android ProgressBar in our application. There are two types of progress bars : Horizontal and Circular. We’ll create both of these progress bar in android application.

Android ProgressBar

Android ProgressBar is a graphical view indicator that shows some progress. Android progress bar displays a bar representing the completing of the task. Progress bar in android is useful since it gives the user an idea of time to finish its task. Using a ProgressBar is a good user experience practice since it displays the status of progress of the given task (such as downloading an image) to the user.

Android ProgressBar attributes

Some important attributes used to describe a ProgressBar are given below.

  1. android:max : We can set the maximum value of the ProgressBar using this attribute. By default the progress bar maximum value is 100
  2. android:indeterminate : A boolean value is set depending on whether the time is determinate or not. Setting this attribute to false would show the actual progress. Else if it’s set to true a cyclic animation is displayed to show that progress is happening
  3. android:minHeight : It’s used to set the height of the ProgressBar
  4. android:minWidth : It’s used to set the width of the ProgressBar
  5. android:progress : It’s used to set the number by which the progress bar value will be incremented
  6. style : By default the progress bar will be displayed as a spinning wheel. If we want it to be displayed as a horizontal bar, we need to set the attribute as : style=“?android:attr/progressBarStyleHorizontal”

In this tutorial we’ll be creating a ProgressBar and increment its values by updating inside a thread. We’ll make the thread to sleep for 200 milliseconds after incrementing the values to show the progress slowly.

Android Progress Bar Example Project Structure

android progressbar, progress bar in android This project consists of a single Activity and layout that contains both the type of Progress Bar.

Android Progress Bar Code

The activity_main.xml contains a RelativeLayout as the parent view which contains a Horizontal ProgressBar and a Circular one along with a TextView to display the progress in numeric terms. activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="20dp"
        android:indeterminate="false"
        android:max="100"
        android:minHeight="50dp"
        android:minWidth="200dp"
        android:progress="1" />

    <ProgressBar
        android:id="@+id/progressBar_cyclic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="50dp"
        android:minWidth="50dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar"
        android:layout_below="@+id/progressBar"/>

</RelativeLayout>

In the above layout, the horizontal progress bar values are updated by one as set in android:progress. The circular progress bar runs continuously unless the activity stops.

package com.journaldev.progressbar;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private ProgressBar progressBar;
    private int progressStatus = 0;
    private TextView textView;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        textView = (TextView) findViewById(R.id.textView);
        // Start long running operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (progressStatus < 100) {
                    progressStatus += 1;
                    // Update the progress bar and display the
                    //current value in the text view
                    handler.post(new Runnable() {
                        public void run() {
                            progressBar.setProgress(progressStatus);
                            textView.setText(progressStatus+"/"+progressBar.getMax());
                        }
                    });
                    try {
                        // Sleep for 200 milliseconds.
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}

In the above code the progressBar updates after every 200 milliseconds and the progress bar updates are set using setProgress(). A handler is used to run the background thread. The thread runs until the progressStatus value reaches 100. The image given below displays the output of a single instance of our application. android progressbar example, progress bar android In this tutorial we’ve created a basic android ProgressBar. We have shown how to add a ProgressBar in a ProgressDialog in a later tutorial. You can download the final Android ProgressBar Project from the below link.

Download Android ProgressBar Project

Reference: developer.android.com doc

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Anupam Chugh

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 31, 2021

I am working on an android app and hope you could help me. In my main activity, I have a recyclerview and on click of an item it takes me to second activity and loads another recyclerview (to load the data, I have a thread that runs in the background and takes 2-5 second to load). So far this is working as expected. But when it loads I want to show a loading screen/progress bar which I am unable to add it. Any help or guidance is much appreciated.

- vs

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 24, 2019

    I am working in android to create a app for voter list .this is my first project any suggest to me

    - Mallika

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      February 1, 2019

      Awesome tut! Is it possible to make the progressbar itself bigger?

      - Phil

        JournalDev
        DigitalOcean Employee
        DigitalOcean Employee badge
        November 23, 2018

        Hello Anupam, I’m Edoardo Burderi from Italy, the application build completed succesfully but crashed immediately after run, I don’t know what happen. Thanks

        - Edoardo Burderi

          JournalDev
          DigitalOcean Employee
          DigitalOcean Employee badge
          October 17, 2018

          I am working on app which shows current light in the room by light sensor. It displays the amount of light in terms of lx. So, I want to show this amount of light on progressbar. As the light will increase or decrease, the progressbar should show progress according to it, Please tell me how to do this.

          - Vivek Bhavsar

            JournalDev
            DigitalOcean Employee
            DigitalOcean Employee badge
            September 25, 2018

            Now that ProgressDialog is deprecated and we are asked to use ProgressBar instead, can you suggest how to include ProgressBar within an alert dialog/ modal dialog?

            - DEEPTHI K

              JournalDev
              DigitalOcean Employee
              DigitalOcean Employee badge
              July 31, 2018

              Thanks to your codes.

              - Pj

                JournalDev
                DigitalOcean Employee
                DigitalOcean Employee badge
                November 2, 2017

                It’s good and I want to go-to next activity after 100 reached So where I have to set the intent code

                - Charan

                  JournalDev
                  DigitalOcean Employee
                  DigitalOcean Employee badge
                  November 2, 2017

                  thank u so mutch

                  - reyho0n

                    JournalDev
                    DigitalOcean Employee
                    DigitalOcean Employee badge
                    May 26, 2017

                    thks for the tutorial!

                    - Eden

                      Try DigitalOcean for free

                      Click below to sign up and get $200 of credit to try our products over 60 days!

                      Sign up

                      Join the Tech Talk
                      Success! Thank you! Please check your email for further details.

                      Please complete your information!

                      Featured on Community

                      Get our biweekly newsletter

                      Sign up for Infrastructure as a Newsletter.

                      Hollie's Hub for Good

                      Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

                      Become a contributor

                      Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

                      Welcome to the developer cloud

                      DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

                      Learn more