How to Set A Progress Bar in Android

In this article I will tell you how to add a progress bar to an Android application.

Introduction

In this article I will tell you how to add a progress bar to an Android application. A progress bar is a visual indicator of the progress of an operation. It displays a bar to the user representing how much work has done; the application can change the length of the bar as it moves forward. A progress bar can also be made indeterminate. In this mode, the progress bar shows a cyclic animation. This mode is used by applications when the length of the task is unknown.

Step 1

First, create a new project using "File" -> "New" -> "Android Application Project":

After opening the new window as shown in the figure, press Enter.

The Application Name, Project Name and Package Name are as shown below:

 newimage.jpg

new2.jpg

Step 2

Open the activity_main.xml file using "ProgressBar/res/layout/activity_main.xml" and update it using the following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="30dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:text="@string/hello_world" />

    <ProgressBar

        android:id="@+id/progressBar1"

        style="?android:attr/progressBarStyleLarge"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_below="@+id/textView1"

        android:layout_marginTop="37dp" />

</RelativeLayout>

Step 3

Open the MainActivity.java file using "ProgressBar/src/com.example.progressbar/MainActivity.java":

package com.example.progressbar;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.widget.ProgressBar;

public class MainActivity extends Activity

{

 ProgressBar myProgressBar1,myProgressBar2;

 int myProgress = 0;

 @Override

 public void onCreate(Bundle savedInstanceState)

 {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    myProgressBar1=(ProgressBar) findViewById(R.id.progressBar1);

    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++;

            myProgressBar1.setProgress(myProgress);

        }

    };

};

}

Step 4

Open the strings.xml file using "ProgressBar/res/values/strings.xml" and update it if needed:

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">ProgressBar</string>

    <string name="hello_world">Progress Bar :</string>

    <string name="menu_settings">Settings</string>

</resources>

Step 5

Open the manifest.xml file using "ProgressBar/ProgressBar_manifest.xml" as shown in the following:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.progress.bar"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.progress.bar.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>     </application>

</manifest>

Step 6

The output will be as shown below:

3.jpg

1.jpg