Android ProgressBar Example

Android ProgressBar Example Code                  

                 We can show the android advancement bar dialog box to show the status of work being carried out e.g. downloading record, breaking down status of work and so on.

In this sample, we are showing the advancement dialog for sham document download operation.

Here we are utilizing android.app.ProgressDialog class to demonstrate the advancement bar. Android ProgressDialog is the subclass of AlertDialog class.

The ProgressDialog class gives strategies to take a shot at advancement bar like setProgress(), setMessage(), setProgressStyle(), setMax(), show() and so on. The advancement scope of Progress Dialog is 0 to 10000.

We should see a basic case to show advancement bar in android.

Android Progress Bar Example by ProgressDialog

We should see a basic sample to make advancement bar utilizing ProgressDialog class.

activity_main.xml

Drag one catch from the pallete, now the activity_main.xml record will resemble this:

File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  
  
    <Button  
        android:id="@+id/button1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentTop="true"  
        android:layout_centerHorizontal="true"  
        android:layout_marginTop="116dp"  
        android:text="download file" />  
  
</RelativeLayout>  


Activity class

How about we compose the code to show the advancement bar dialog box.
  1. package com.dheeruapps.progressbar1;  
  2. import android.app.Activity;  
  3. import android.app.ProgressDialog;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.widget.Button;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. public class MainActivity extends Activity {  
  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 = (Button) findViewById(R.id.button1);  
  24.         btnStartProgress.setOnClickListener(new 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 {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  46.                   // Updating the progress bar  
  47.                   progressBarHandler.post(new Runnable() {  
  48.                     public void run() {  
  49.                       progressBar.setProgress(progressBarStatus);  
  50.                     }  
  51.                   });  
  52.                 }  
  53.                 // performing operation if file is downloaded,  
  54.                 if (progressBarStatus >= 100) {  
  55.                     // sleeping for 1 second after operation completed  
  56.                     try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  57.                     // close the progress bar dialog  
  58.                     progressBar.dismiss();  
  59.                 }  
  60.               }  
  61.              }).start();  
  62.             }//end of onClick method  
  63.           });  
  64.          }  
  65.     // checking how much file is downloaded and updating the filesize   
  66.     public int doOperation() {  
  67.         //The range of ProgressDialog starts from 0 to 10000  
  68.         while (fileSize <= 10000) {  
  69.             fileSize++;  
  70.             if (fileSize == 1000) {  
  71.                 return 10;  
  72.             } else if (fileSize == 2000) {  
  73.                 return 20;  
  74.             } else if (fileSize == 3000) {  
  75.                 return 30;  
  76.             } else if (fileSize == 4000) {  
  77.             return 40;//you can add more else if  
  78.             } else{  
  79.                 return 100;  
  80.             }  
  81.         }//end of while  
  82.         return 100;  
  83.     }//end of doOperation  
  84.   
  85.     @Override  
  86.     public boolean onCreateOptionsMenu(Menu menu) {  
  87.         // Inflate the menu; this adds items to the action bar if it is present.  
  88.         getMenuInflater().inflate(R.menu.main, menu);  
  89.         return true;  
  90.     }  
  91. }  

Output:




0 comments:

Post a Comment

My Instagram