Android Progress Bar using ProgressDialog Tutorial


Progress bars are used to show progress of a task. For example. When you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.
In android there is a class called ProgressDialog that allow you to create progress bar. In order to this , you need to instantiate an object of this class. Its syntax is.
ProgressDialog progress = new ProgressDialog(this);
Now you can set some properties of this dialog. Such as , its style,its text e.t.c
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
Apart from these methods, there are other methods that are provided by the ProgressDialog class
Sr. NOTitle and description
1getMax()
This methods returns the maximum value of the progress
2incrementProgressBy(int diff)
This method increment the progress bar by the difference of value passed as a parameter
3setIndeterminate(boolean indeterminate)
This method set the progress indicator as determinate or indeterminate
4setMax(int max)
This method set the maximum value of the progress dialog
5setProgress(int value)
This method is used to update the progress dialog with some specific value
6show(Context context, CharSequence title, CharSequence message)
This is a static method , used to display progress dialog

Example

This example demonstrates the horizontol use of the progress dialog which is infact a progress bar. It display a progress bar on pressing the button.
To experiment with this example , you need to run this on an actual device on after developing the application according to the steps below.
StepsDescription
1You will use Eclipse IDE to create an Android application and name it as ProgressDialog under a package com.example.progressdialog. While creating this project, make sure you Target SDK and Compile With at the latest version of Android SDK to use higher levels of APIs.
2Modify src/MainActivity.java file to add progress code to display the progress dialog.
3Modify res/layout/activity_main.xml file to add respective XML code.
4Modify res/values/string.xml file to add a message as a string constant.
5Run the application and choose a running android device and install the application on it and verify the results.
Following is the content of the modified main activity filesrc/com.example.progressdialog/MainActivity.java.
package com.example.progressdialog;

import com.example.progressdialog.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

   private ProgressDialog progress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progress = new ProgressDialog(this);
   }


   public void open(View view){
      progress.setMessage("Downloading Music :) ");
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.show();

   final int totalProgressTime = 100;

   final Thread t = new Thread(){

   @Override
   public void run(){
 
      int jumpTime = 0;
      while(jumpTime < totalProgressTime){
         try {
            sleep(200);
            jumpTime += 5;
            progress.setProgress(jumpTime);
         } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
         }

      }

   }
   };
   t.start();

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}
Modify the content of res/layout/activity_main.xml to the following
<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"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="150dp"
      android:onClick="open"
      android:text="@string/download_button" />

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_alignParentTop="true"
      android:layout_marginTop="19dp"
      android:text="@string/download_text"
      android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Modify the res/values/string.xml to the following
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">ProgressDialog</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>
   <string name="download_button">Download</string>
   <string name="download_text">Press the button to download music</string>
</resources>
This is the default AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.progressdialog"
   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.example.progressdialog.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>
Let's try to run your ProgressDialog application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Eclipse, open one of your project's activity files and click Run Eclipse Run Icon icon from the toolbar. Before starting your application, Eclipse will display following window to select an option where you want to run your Android application.
Anroid Camera Tutorial
Select your mobile device as an option and then check your mobile device which will display following screen:
Android Progress Dialog Tutorial
Just press the button to start the Progress bar. After pressing , following screen would appear
Android Progress Dialog Tutorial
It will continously update it self , and after few seconds , it would appear something like this.
And roid Progress Dialog Tutorial

Comments

Popular Posts