Skip to main content

Android ProgressBar Example

We can display the progress bar dialog box to display the status of work being done e.g. downloading file etc.
In this example, we are displaying the progress dialog for dummy file download.
The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), show() etc.

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 com.example.progressbar1;  
  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.   
  12. public class MainActivity extends Activity {  
  13.     Button btnStartProgress;  
  14.     ProgressDialog progressBar;  
  15.     private int progressBarStatus = 0;  
  16.     private Handler progressBarHandler = new Handler();  
  17.     private long fileSize = 0;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         addListenerOnButtonClick();  
  24.     }  
  25.       
  26.     public void addListenerOnButtonClick() {  
  27.            
  28.         btnStartProgress = (Button) findViewById(R.id.button1);  
  29.         btnStartProgress.setOnClickListener(new OnClickListener(){  
  30.    
  31.            @Override  
  32.            public void onClick(View v) {  
  33.    
  34.             // creating progress bar dialog  
  35.             progressBar = new ProgressDialog(v.getContext());  
  36.             progressBar.setCancelable(true);  
  37.             progressBar.setMessage("File downloading ...");  
  38.             progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  39.             progressBar.setProgress(0);  
  40.             progressBar.setMax(100);  
  41.             progressBar.show();  
  42.    
  43.             //reset progress bar and filesize status  
  44.             progressBarStatus = 0;  
  45.             fileSize = 0;  
  46.    
  47.             new Thread(new Runnable() {  
  48.               public void run() {  
  49.                 while (progressBarStatus < 100) {  
  50.    
  51.                   // performing operation  
  52.                   progressBarStatus = doOperation();  
  53.    
  54.                   try {  
  55.                     Thread.sleep(1000);  
  56.                   } catch (InterruptedException e) {  
  57.                     e.printStackTrace();  
  58.                   }  
  59.    
  60.                   // Updating the progress bar  
  61.                   progressBarHandler.post(new Runnable() {  
  62.                     public void run() {  
  63.                       progressBar.setProgress(progressBarStatus);  
  64.                     }  
  65.                   });  
  66.                 }  
  67.    
  68.                 // performing operation if file is downloaded,  
  69.                 if (progressBarStatus >= 100) {  
  70.    
  71.                     // sleeping for 1 second after operation completed  
  72.                     try {  
  73.                         Thread.sleep(1000);  
  74.                     } catch (InterruptedException e) {e.printStackTrace();}  
  75.    
  76.                     // close the progress bar dialog  
  77.                     progressBar.dismiss();  
  78.                 }  
  79.               }  
  80.              }).start();  
  81.             }//end of onClick method  
  82.           });  
  83.          }  
  84.    
  85.     // checking how much file is downloaded and updating the filesize   
  86.     public int doOperation() {  
  87.         //The range of ProgressDialog starts from 0 to 10000  
  88.         while (fileSize <= 10000) {  
  89.             fileSize++;  
  90.             if (fileSize == 1000) {  
  91.                 return 10;  
  92.             } else if (fileSize == 2000) {  
  93.                 return 20;  
  94.             } else if (fileSize == 3000) {  
  95.                 return 30;  
  96.             } else if (fileSize == 4000) {  
  97.             return 40;  
  98.             } else if (fileSize == 5000) {  
  99.                 return 50;  
  100.             } else if (fileSize == 6000) {  
  101.                 return 60;  
  102.             }  
  103.              else if (fileSize == 7000) {  
  104.                     return 70;  
  105.             }  
  106.              else if (fileSize == 8000) {  
  107.                     return 80;  
  108.             }  
  109.              else if (fileSize == 9000) {  
  110.                     return 90;  
  111.             }  
  112.              else if (fileSize == 10000) {  
  113.                     return 100;  
  114.             }  
  115.         }//end of while  
  116.         return 100;  
  117.     }//end of doOperation  
  118.   
  119.     @Override  
  120.     public boolean onCreateOptionsMenu(Menu menu) {  
  121.         // Inflate the menu; this adds items to the action bar if it is present.  
  122.         getMenuInflater().inflate(R.menu.main, menu);  
  123.         return true;  
  124.     }  
  125. }  


Output:

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

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework

            MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework ABSTRACT  In recent years, recommendation systems have seen significant evolution in the field of knowledge engineering. Most of the existing recommendation systems based their models on collaborative filtering approaches that make them simple to implement. However, performance of most of the existing collaborative filtering-based recommendation system suffers due to the challenges, such as: (a) cold start, (b) data sparseness, and (c) scalability. Moreover, recommendation problem is often characterized by the presence of many conflicting objectives or decision variables, such as users’ preferences and venue closeness. In this paper, we proposed MobiContext , a hybrid cloud-based Bi-Objective Recommendation Framework (BORF) for mobile social networks. The MobiContext utilizes multi-objective optimization techniques to generate personalized recommendat...