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...

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search ABSTRACT: In this paper, With advances in geo-positioning technologies and geo-location services, there are a rapidly growing amount of spatiotextual objects collected in many applications such as location based services and social networks, in which an object is described by its spatial location and a set of keywords (terms). Consequently, the study of spatial keyword search which explores both location and textual description of the objects has attracted great attention from the commercial organizations and research communities. In the paper, we study two fundamental problems in the spatial keyword queries: top k spatial keyword search (TOPK-SK), and batch top k spatial keyword search (BTOPK-SK). Given a set of spatio-textual objects, a query location and a set of query keywords, the TOPK-SK retrieves the closest k objects each of which contains all keywords in the query. BTOPK-SK is the batch processing of sets...