Sunday, July 10, 2011

How to Display Progress Bar in Android

In many cases we need to display the progress bar, like loading the apps, performing new task, closing apps and more. In order to perform you can use the below code to program your android apps to show the progress bar.

public class MyActivity extends Activity {
private static final int PROGRESS = 0x1;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.progressbar_activity);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) { mProgressStatus = doWork(); // Here we updating progress bar mHandler.post(new Runnable() { public void run() { mProgress.setProgress(mProgressStatus); } }); } } }).start(); } } Add the below line in AndroidManifast file to display the progress bar.


No comments:

Post a Comment