Saturday, February 15, 2014

Android ProgressBar.

activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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="34dp"
        android:text="@string/b1" />

</RelativeLayout>


ActivityMain.java


package com.mahi.progressbar;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button b;  
ProgressDialog pb;  
private int progressBarStatus = 0;  
private Handler progressBarHandler = new Handler();  
private long fileSize = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}

public void addListenerOnButtonClick() {  
         
       b = (Button) findViewById(R.id.button1);  
       b.setOnClickListener(new OnClickListener(){  
  
          @Override  
          public void onClick(View v) {
  
           // creating progress bar dialog  
           pb = new ProgressDialog(v.getContext());  
           pb.setCancelable(true);  
           pb.setMessage("File downloading ...");  
           pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
           pb.setProgress(0);  
           pb.setMax(100);  
           pb.show();  
  
           //reset progress bar and filesize status  
           progressBarStatus = 0;  
           fileSize = 0;  
  
           new Thread(new Runnable() {  
             public void run() {  
               while (progressBarStatus < 100) {  
  
                 // performing operation  
                 progressBarStatus = doOperation();  
  
                 try {  
                   Thread.sleep(1000);  
                 } catch (InterruptedException e) {  
                   e.printStackTrace();  
                 }  
  
                 // Updating the progress bar  
                 progressBarHandler.post(new Runnable() {  
                   public void run() {  
                     pb.setProgress(progressBarStatus);  
                   }  
                 });  
               }  
   
               if (progressBarStatus >= 100) {  
  
                   // sleeping for 1 second after operation completed  
                   try {  
                       Thread.sleep(1000);  
                   } catch (InterruptedException e) {e.printStackTrace();}  
    
                   pb.dismiss();  
               }  
             }  
            }).start();  
           }//end of onClick method  
         });  
        }  
  
   public int doOperation() {

       while (fileSize <= 10000) {  
           fileSize++;  
           if (fileSize == 1000) {  
               return 10;  
           }
           else if (fileSize == 2000) {  
               return 20;  
           }
           else if (fileSize == 3000) {  
               return 30;  
           }
           else if (fileSize == 4000) {  
            return 40;  
           }
           else if (fileSize == 5000) {  
               return 50;  
           }
           else if (fileSize == 6000) {  
               return 60;  
           }  
           else if (fileSize == 7000) {  
               return 70;  
           }  
           else if (fileSize == 8000) {  
               return 80;  
           }  
           else if (fileSize == 9000) {  
               return 90;  
           }  
           else if (fileSize == 10000) {  
               return 100;  
           }  
       }
       return 100;
   }
   
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Output:



No comments:

Post a Comment