Tuesday, March 16, 2021

Android ProgressBar

 

Android ProgressBar Example

android progress dialog

We can display the android progress bar dialog box to display the status of work being done e.g. downloading file, analyzing status of work etc.

In this example, we are displaying the progress dialog for dummy file download operation.

Here we are using android.app.ProgressDialog class to show the progress bar. Android ProgressDialog is the subclass of AlertDialog class.

The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), setProgressStyle(), setMax(), show() etc. The progress range of Progress Dialog is 0 to 10000.


Let's see a simple example to display progress bar in android.

  1. ProgressDialog progressBar = new ProgressDialog(this);  
  2. progressBar.setCancelable(true);//you can cancel it by pressing back button  
  3. progressBar.setMessage("File downloading ...");  
  4. progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  5. progressBar.setProgress(0);//initially progress is 0  
  6. progressBar.setMax(100);//sets the maximum value 100  
  7. progressBar.show();//displays the progress bar  


Android Progress Bar Example by ProgressDialog

Let's see a simple example to create progress bar using ProgressDialog class.

activity_main.xml

Drag one button from the pallete, now the activity_main.xml file will look like this:

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

Activity class

Let's write the code to display the progress bar dialog box.

File: MainActivity.java
  1. package example.javatpoint.com.progressbar;  
  2.   
  3. import android.app.ProgressDialog;  
  4. import android.os.Handler;  
  5. import android.support.v7.app.AppCompatActivity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.     Button btnStartProgress;  
  12.     ProgressDialog progressBar;  
  13.     private int progressBarStatus = 0;  
  14.     private Handler progressBarHandler = new Handler();  
  15.     private long fileSize = 0;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         addListenerOnButtonClick();  
  21.     }  
  22.     public void addListenerOnButtonClick() {  
  23.         btnStartProgress = findViewById(R.id.button);  
  24.         btnStartProgress.setOnClickListener(new View.OnClickListener(){  
  25.   
  26.             @Override  
  27.             public void onClick(View v) {  
  28.                 // creating progress bar dialog  
  29.                 progressBar = new ProgressDialog(v.getContext());  
  30.                 progressBar.setCancelable(true);  
  31.                 progressBar.setMessage("File downloading ...");  
  32.                 progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  33.                 progressBar.setProgress(0);  
  34.                 progressBar.setMax(100);  
  35.                 progressBar.show();  
  36.                 //reset progress bar and filesize status  
  37.                 progressBarStatus = 0;  
  38.                 fileSize = 0;  
  39.   
  40.                 new Thread(new Runnable() {  
  41.                     public void run() {  
  42.                         while (progressBarStatus < 100) {  
  43.                             // performing operation  
  44.                             progressBarStatus = doOperation();  
  45.                             try {  
  46.                                 Thread.sleep(1000);  
  47.                             } catch (InterruptedException e) {  
  48.                                 e.printStackTrace();  
  49.                             }  
  50.                             // Updating the progress bar  
  51.                             progressBarHandler.post(new Runnable() {  
  52.                                 public void run() {  
  53.                                     progressBar.setProgress(progressBarStatus);  
  54.                                 }  
  55.                             });  
  56.                         }  
  57.                         // performing operation if file is downloaded,  
  58.                         if (progressBarStatus >= 100) {  
  59.                             // sleeping for 1 second after operation completed  
  60.                             try {  
  61.                                 Thread.sleep(1000);  
  62.                             } catch (InterruptedException e) {  
  63.                                 e.printStackTrace();  
  64.                             }  
  65.                             // close the progress bar dialog  
  66.                             progressBar.dismiss();  
  67.                         }  
  68.                     }  
  69.                 }).start();  
  70.             }//end of onClick method  
  71.         });  
  72.     }  
  73.     // checking how much file is downloaded and updating the filesize  
  74.     public int doOperation() {  
  75.         //The range of ProgressDialog starts from 0 to 10000  
  76.         while (fileSize <= 10000) {  
  77.             fileSize++;  
  78.             if (fileSize == 1000) {  
  79.                 return 10;  
  80.             } else if (fileSize == 2000) {  
  81.                 return 20;  
  82.             } else if (fileSize == 3000) {  
  83.                 return 30;  
  84.             } else if (fileSize == 4000) {  
  85.                 return 40// you can add more else if   
  86.             }   
  87.          /* else { 
  88.                 return 100; 
  89.             }*/  
  90.         }//end of while  
  91.             return 100;  
  92.     }//end of doOperation  
  93. }  

Output:

android progress bar example output 1 android progress bar example output 2

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile