How to Set A Progress Bar in Android

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:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="30dp"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:text="@string/hello_world" />  
  14.     <ProgressBar  
  15.         android:id="@+id/progressBar1"  
  16.         style="?android:attr/progressBarStyleLarge"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_below="@+id/textView1"  
  21.         android:layout_marginTop="37dp" />  
  22. </RelativeLayout> 
Step 3
 
Open the MainActivity.java file using "ProgressBar/src/com.example.progressbar/MainActivity.java":
  1. package com.example.progressbar;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Handler;  
  5. import android.os.Message;  
  6. import android.widget.ProgressBar;  
  7. public class MainActivity extends Activity  
  8. {  
  9.  ProgressBar myProgressBar1,myProgressBar2;  
  10.  int myProgress = 0;  
  11.  @Override  
  12.  public void onCreate(Bundle savedInstanceState)  
  13.  {  
  14.     super.onCreate(savedInstanceState);  
  15.     setContentView(R.layout.activity_main);  
  16.     myProgressBar1=(ProgressBar) findViewById(R.id.progressBar1);  
  17.     new Thread(myThread).start();  
  18.  }  
  19. private Runnable myThread = new Runnable()  
  20. {  
  21.     @Override  
  22.     public void run()  
  23.     {  
  24.       // TODO Auto-generated method stub  
  25.         while (myProgress<100)  
  26.         {  
  27.             try{  
  28.                 myHandle.sendMessage(myHandle.obtainMessage());  
  29.                 Thread.sleep(1000);  
  30.             }  
  31.             catch(Throwable t)  
  32.             {   }  
  33.         }  
  34.     }  
  35.    Handler myHandle = new Handler()  
  36.    {  
  37.         @Override  
  38.         public void handleMessage(Message msg)  
  39.         {  
  40.             // TODO Auto-generated method stub  
  41.             myProgress++;  
  42.             myProgressBar1.setProgress(myProgress);  
  43.         }  
  44.     };  
  45. };  
Step 4
 
Open the strings.xml file using "ProgressBar/res/values/strings.xml" and update it if needed:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">ProgressBar</string>  
  4.     <string name="hello_world">Progress Bar :</string>  
  5.     <string name="menu_settings">Settings</string>  
  6. </resources> 
Step 5
 
Open the manifest.xml file using "ProgressBar/ProgressBar_manifest.xml" as shown in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.progress.bar"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name="com.progress.bar.MainActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>     </application>  
  22. </manifest> 
Step 6
 
The output will be as shown below:
 
3.jpg
 
1.jpg


Similar Articles