Powered By Blogger

Monday 27 February 2012

ProgressBar


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ProgressBar extends Activity {

//String items[]={"Googel","Appel","MacBook"};
//boolean[] itemchecked= new boolean[items.length];
/*ProgressThread progthread;
ProgressThread progThread;
*/
ProgressDialog progressdialog;
int progress=0;
Handler progresshandler;
Button b1;
Dialog dialog;
static int maxBarValue = 200;
//ProgressThread progressthread;
//protected Message Message;
    /** Called when the activity is first created. */
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progresshandler=new Handler()
        {
        public void handleMessage(Message msg)
        {
        super.handleMessage(msg);
        if (progress >= 100) {
        Toast.makeText(getApplicationContext(),"Sucess!!", Toast.LENGTH_LONG).show();
        progressdialog.dismiss();
        } else {
        progress++;
        progressdialog.incrementProgressBy(1);
        progresshandler.sendEmptyMessageDelayed(0, 100);
        }
        }
       
        };
       
       
       
 
       
       
       
       
     
       
        progressdialog=new ProgressDialog(this);
        progressdialog.setTitle("Downloading files");
        progressdialog.setProgressStyle(progressdialog.STYLE_HORIZONTAL);
        progressdialog.setButton(DialogInterface.BUTTON_POSITIVE,"Hide",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Hide Clicked!!",Toast.LENGTH_LONG).show();
}
});
        progressdialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel Clicked!!",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"Cancel Clicked!!",Toast.LENGTH_LONG).show();

}
});
       
           
       
     
       
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener()
        {

@Override
public void onClick(View v) {
progressdialog.show();
progress=0;
/*Thread t=new Thread();
t.start();*/
ProgressThread progressthread=new ProgressThread(progresshandler);
progressthread.start();


progressdialog.setProgress(0);
progresshandler.sendEmptyMessage(0);
}

});
       
       
       
     
       
     
    }
}


class ProgressThread extends Thread
{

final static int DONE = 0;
    final static int RUNNING = 1;
   
    Handler mHandler;
    int mState;
    int total;
    ProgressThread(Handler m)
    {
    mHandler=m;
    }
    public void run() {
        mState = RUNNING;  
        total = ProgressBar.maxBarValue;
        while (mState == RUNNING) {
           
            try {
                // Control speed of update (but precision of delay not guaranteed)
                Thread.sleep(40);
               
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           
         
           
            total--;  



}}}

No comments:

Post a Comment