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

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM

A SHOULDER SURFING RESISTANT GRAPHICAL AUTHENTICATION SYSTEM ABSTRACT Authentication based on passwords is used largely in applications for computer security and privacy. However, human actions such as choosing bad passwords and inputting passwords in an insecure way are regarded as”the weakest link” in the authentication chain. Rather than arbitrary alphanumeric strings, users tend to choose passwords either short or meaningful for easy memorization. With web applications and mobile apps piling up, people can access these applications anytime and anywhere with various devices. This evolution brings great convenience but also increases the probability of exposing passwords to shoulder surfing attacks. Attackers can observe directly or use external recording devices to collect users’ credentials. To overcome this problem, we proposed a novel authentication system PassMatrix, based on graphical passwords to resist shoulder surfing attacks. With a one-time valid login indicator and ...

ABSTRACT

MULTI USER GSM BASED DIGITAL SCROLLING LED DISPLAY WITH PASSWORD AND VOICE ABSTRACT The project mainly focuses on transmission of textual data through air interface by the use of GSM through asynchronous serial communication. The data will be processed by the microcontroller it programmed by embedded c language by using KEIL C compiler. The data will be displayed on led display on LED only after entering unique pass key. Existing System: How a reliable and an authentic wireless communication could be easily developed between a mobile phone and microcontroller using GSM (Global System for Mobile Communication) MODEM (Modulator-Demodulator). This technical paper explains GSM based e-notice board which can be widely used for multitude of applications including educational sector, traffic control, banks, and public advertisements, stoke exchanges etc. Proposed System: In this project we not only send the data, but send the data with pass code also. Which enables us to preve...