Monday, June 18, 2012

How To Show Progress Bar In Your Android Application



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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        />
        <ProgressBar android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressbar_default"
        />
        <ProgressBar android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:id="@+id/progressbar_Horizontal"
        android:max="100" />
</LinearLayout>
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:

No comments:

Post a Comment