Android – ProgressBar View

The ProgressBar view provides visual information about certain tasks in progress, such as when you perform a task in the background. For example, you might be downloading data from the Web and should notify the user of the download status. In this case, the ProgressBar the view is a good choice. The following activity shows how to use the ProgressBar view.

Step 1. Using Android Studio, create an Android project and name it ProgressBar:

progressbar

Step 2. Modify the activity_main.xml file located in the res/layout folder by adding the following code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<ProgressBar 
         android:id="@+id/progressbar"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" />
</LinearLayout>

Step 3. In the MainActivity.java file, add the following statements:

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
 static int progress;
 ProgressBar progressBar;
 int progressStatus = 0;
 Handler handler = new Handler();
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
       progress = 0;
       progressBar = (ProgressBar) 
       findViewById(R.id.progressbar);
 //---do some work in background thread---
 new Thread(new Runnable()
 {
 public void run()
 {
 //---do some work here---
 while (progressStatus < 10)
 {
 progressStatus = doSomeWork();
 }
 //---hides the progress bar---
 handler.post(new Runnable()
 {
 public void run()
 {
 //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
 progressBar.setVisibility(View.GONE);
 }
 });
 }
 //---do some long running work here---
 private int doSomeWork()
 {
 try {
 //---simulate doing some work---
 Thread.sleep(500);
 } catch (InterruptedException e)
 {
 e.printStackTrace();
 }
 return ++progress;
 }
 }).start();
 }
}

Step 4. Press Shift+F9 to debug the project on the Android emulator. Shows the ProgressBar animating. After about five seconds, it disappears.

20201207 221959

How It Works

The default mode of the ProgressBar view is indeterminate, that is, it displays cyclic animation. This mode is useful for tasks that do not have specific run times, such as when you send some data to a web service and awaiting response from the server. If you just put the item in your main.xml file, it permanently displays a rotating icon. It is your responsibility to stop when your background task is complete.

The code added to the MainActivity.java file shows how to create a background thread for simulate the execution of certain long-term tasks. To do this, use the Thread class with a Runnable object.The run () method starts the execution of the thread, which in this case calls the do Some Work () function method to simulate a job. When the simulated job is finished (after about five seconds), use a Handler object to send a message to the thread to reject ProgressBar:

new Thread(new Runnable()
 {

 public void run()
 {
 //---do some work here---
 while (progressStatus < 10)
 {
 progressStatus = doSomeWork();
 }
 //---hides the progress bar---
 handler.post(new Runnable()
 {
 public void run()
 {
 //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
 progressBar.setVisibility(View.GONE);
 }
 });
 }
 //---do some long running work here---
 private int doSomeWork()
 {
 try {
 //---simulate doing some work---
 Thread.sleep(500);
 } catch (InterruptedException e)
 {
 e.printStackTrace();
 }
 return ++progress;
 }
 }).start();

When the task is completed, hide the ProgressBar by setting its Visibility property to View.GONE (value 8). There are two differences between the INVISIBLE constant and the GONE constant:
            ➤ INVISIBLE constant simply hides the ProgressBar (the region occupied by the ProgressBar is still taking up space in the activity).
            ➤ GONE constant removes the ProgressBar view from the activity and does not take up any space.