Progress Bar Android App Using Android Studio

Introduction

In this article, I will show you how to create a Progress Bar Android app using Android Studio. We can display the Android progress bar dialog box to display the status of the work being done, e.g., downloading a file or analyzing the status of work etc.

 Requirements

Steps should be followed

Follow these steps to create Progress Bar app using Android Studio. I have attached the source code too.

Step 1

Open Android Studio and start a New Android Studio Project.

Progress bar Android App

Step 2

You can choose your application name and choose where your project is stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Progress bar Android App

Step 3

Now, select the version of Android and select the target Android devices. We need to choose the SDK level which plays an important role to run the application.

Progress bar Android App

Step 4

Now, add the activity and click "Next" button.

Progress bar Android App

Step 5

Add Activity name and click "Finish". 

Progress bar Android App

Step 6

Go to activity_main.xml, This XML file contains the designing code for our Android app.

Progress bar Android App

The XML code is given below.  

  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/button1"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_marginTop="116dp"  
  15.         android:text="download file" />  
  16.   
  17. </RelativeLayout>  

Step 7

Go to Main Activity.java. This Java program is the backend language for Android.

Progress bar Android App

The java code is given below.

  1. package abu.progressbar;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.widget.Button;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. public class MainActivity extends Activity {  
  12.     Button btnStartProgress;  
  13.     ProgressDialog progressBar;  
  14.     private int progressBarStatus = 0;  
  15.     private Handler progressBarHandler = new Handler();  
  16.     private long fileSize = 0;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.         addListenerOnButtonClick();  
  22.     }  
  23.     public void addListenerOnButtonClick() {  
  24.         btnStartProgress = (Button) findViewById(R.id.button1);  
  25.         btnStartProgress.setOnClickListener(new OnClickListener(){  
  26.   
  27.             @Override  
  28.             public void onClick(View v) {  
  29.                 
  30.                 progressBar = new ProgressDialog(v.getContext());  
  31.                 progressBar.setCancelable(true);  
  32.                 progressBar.setMessage("File downloading ...");  
  33.                 progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  34.                 progressBar.setProgress(0);  
  35.                 progressBar.setMax(100);  
  36.                 progressBar.show();  
  37.                   
  38.                 progressBarStatus = 0;  
  39.                 fileSize = 0;  
  40.   
  41.                 new Thread(new Runnable() {  
  42.                     public void run() {  
  43.                         while (progressBarStatus < 100) {  
  44.                            
  45.                             progressBarStatus = doOperation();  
  46.                             try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  47.                             progressBarHandler.post(new Runnable() {  
  48.                                 public void run() {  
  49.                                     progressBar.setProgress(progressBarStatus);  
  50.                                 }  
  51.                             });  
  52.                         }  
  53.                           
  54.                         if (progressBarStatus >= 100) {  
  55.                             try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  56.                             progressBar.dismiss();  
  57.                         }  
  58.                     }  
  59.                 }).start();  
  60.             }   
  61.         });  
  62.     }  
  63.       
  64.     public int doOperation() {  
  65.           
  66.         while (fileSize <= 10000) {  
  67.             fileSize++;  
  68.             if (fileSize == 1000) {  
  69.                 return 10;  
  70.             } else if (fileSize == 2000) {  
  71.                 return 20;  
  72.             } else if (fileSize == 3000) {  
  73.                 return 30;  
  74.             } else if (fileSize == 4000) {  
  75.                 return 40;  
  76.             } else{  
  77.                 return 100;  
  78.             }  
  79.         }  
  80.         return 100;  
  81.     }  
  82.   
  83.     @Override  
  84.     public boolean onCreateOptionsMenu(Menu menu) {  
  85.          
  86.         getMenuInflater().inflate(R.menu.main, menu);  
  87.         return true;  
  88.     }  
  89.   
  90. }  

Step 8

Now, go to menu bar and click "make project" or press ctrl+f9 to debug the error.

Progress bar Android App

Step 9

Then, click the Run button or press shift+f10 to run the project. Choose the virtual machine option and click OK.

Progress bar Android App

Conclusion

We have successfully created the Progress Bar Android application using Android Studio.

Progress bar Android App

Progress bar Android App

Up Next
    Ebook Download
    View all
    Learn
    View all