Progress Bar

This is a sample program to create a progress bar for your app.

Underlying Algorithm:
Basic description of algorithm in step by step form:

1.) Create a Project ProgressBarExample
2.) Put the following code in res/layout/main.xml:

 

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100" />

 

 

3.) Run the application.

Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project. Give the respective project name i.e. ProgressBarExample. Enter following information:
Project name: ProgressBarExample
Build Target: Android 2.3
Application name: ProgressBarExample
Package name: com.example.ProgressBarExample
Create Activity: ProgressBarExample


On Clicking Finish ProgressBarExample code structure is generated with the necessary Android Packages being imported along with ProgressBarExample.java. ProgressBarExample class will look like following:


package com.example.ProgressBarExample;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class ProgressBarExample extends Activity {

ProgressBar myProgressBar;
int myProgress = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);

new Thread(myThread).start();
}

private Runnable myThread = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
while (myProgress<100){
try{
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(1000);
}
catch(Throwable t){ }
}
}

Handler myHandle = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
}

 

 


Output
–The final output:

Leave a Comment:

5 comments
Anil says June 1, 2011

Thanks for this example.

Reply
latif` says February 20, 2013

thank you

Reply
ayaz says February 20, 2013

not working this program

Reply
Johan Quijano says February 26, 2013

Greetings,

When the “while” finish, Could I add a Intent ? for pass to other activity ?

Thanks

Reply
Om says April 4, 2013

how to stop progressbar and where to write the code to stop it

Reply
Add Your Reply