Adding a progressbar

Adding a Progressbar

package com.dz.supernova;
import android.app.Activity;
import android.app.AlertDialog;
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.widget.Button;
import android.widget.Toast;

public class SupernovaActivity extends Activity {
/** Called when the activity is first created. */
private ProgressDialog prgdiag;
private int progress = 0;
private Handler progHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Test", Toast.LENGTH_SHORT).show();
showDialog(0);
progress=0;
prgdiag.setProgress(0);
progHandler.sendEmptyMessage(0);
}
});

progHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progress>=100){
prgdiag.dismiss();
}
else {
progress++;
prgdiag.incrementProgressBy(1);
progHandler.sendEmptyMessageDelayed(0, 100);
}
}
};

}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {

case 0:
prgdiag = new ProgressDialog(this);
// prgdiag.setIcon(R.drawable.icon);
prgdiag.setTitle("Downloading files...");
prgdiag.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prgdiag.setButton(DialogInterface.BUTTON_POSITIVE, "Hide", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Hide clicked!", Toast.LENGTH_SHORT).show();
}
});
prgdiag.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
"Cancel clicked!", Toast.LENGTH_SHORT).show();
}
});
return prgdiag;
}
return null;
}
}

[/code]


You are reading this post on Joel G Mathew’s tech blog. Joel's personal blog is the Eyrie, hosted here.