ContentLoadingProgressBar

ContentLoadingProgressBar is used to display the status of work being done like analyzing status of work or downloading a file etc. In Android, by default a progress bar will be displayed as a spinning wheel but If we want it to be displayed as a horizontal bar then we need to use style attribute as horizontal. It mainly use the androidx.core.widget.ContentLoadingProgressBar class. As ContentLoadingProgressBar is advance version of ProgressBar, we recommend to use ContentLoadingProgressBar.

public class ContentLoadingProgressBar extends ProgressBar
java.lang.Object
↳ android.view.View
  ↳ android.widget.ProgressBar
    ↳ androidx.core.widget.ContentLoadingProgressBar

We Recommend to use new UI components i.e. androidx components


Create ContentLoadingProgressBar in xml:

<androidx.core.widget.ContentLoadingProgressBar
    android:id="@+id/progressBar"
    android:layout_width="120dp"
    android:layout_height="120dp"
    style="?android:attr/progressBarStyleLarge"
    android:layout_centerInParent="true" />


Access in Activity class:

final ContentLoadingProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.show();

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
	progressBar.hide();
    }
}, 2000);

In above example ContentLoadingProgressBar hide after 2 Seconds.

Horizontal progress bar example in xml:

<androidx.core.widget.ContentLoadingProgressBar
    android:id="@+id/progressBar1"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:indeterminate="true"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_centerInParent="true" />


In the below code the progressBar updates after every 1000 milliseconds and the progress bar updates are set using setProgress().

ProgressBarActivity.java
package com.theitbulls.uicomponentex;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.widget.ContentLoadingProgressBar;

import java.util.Timer;
import java.util.TimerTask;

public class ProgressBarActivity extends AppCompatActivity {

    private Timer timer;
    private ContentLoadingProgressBar progressBar;
    private AppCompatTextView tvStatus;

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

        progressBar = findViewById(R.id.progressBar1);
        progressBar.show();

        progressBar.setMax(200);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                progressBar.hide();
                Toast.makeText(ProgressBarActivity.this, "Task Completed", Toast.LENGTH_SHORT).show();
            }
        }, 10000);

        tvStatus = findViewById(R.id.tvStatus);

        startTimer();
    }

    private void startTimer() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            private int count = 1;

            @Override
            public void run() {
                while (true && timer != null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            int totalComplete = (int) (200 * (.10 * (count++)));
                            tvStatus.setText(totalComplete + "/" + progressBar.getMax());
                            progressBar.setProgress(totalComplete);
                        }
                    });
                }
            }
        }, 0);
    }
}