Android Bar Styled Progress Dialog Example

By | April 7, 2013
Android SDK Provides “android.app.ProgressDialog” class for creating Progress Dialog ,Progress Dialog is a user Interface which is usually used to keep end user engage when performing long running progress . In this tutorial we will see how to creating progress dialog having  horizontal progressing style .
Android Progress Dialog

Also Read : Android Spinning ProgressDialog .

Constructing Progress Dialog

To construct a progress dialog we make use of “ProgressDialog(Context context)”class

                      ProgressDialog  pro_dialog = new ProgressDialog(this);

 Title:- To set the title we call “setTitle(CharSequence title)” upon ProgressDialog class object                          

 pro_dialog.setTitle(“Title”);               

   
 ProgressStyle:- To set progress style we call “setProgressStyle(int style)” upon ProgressDialog class object ,style can be horizontal or spining kind    
                
                 pro_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

 Max :- To set the Progress length we call “setMax(int max)” upon ProgressDialog class object       

 pro_dialog.setMax(“100”);

   
 Button:- To set the button we call “setButton(int whichButton, CharSequence text, OnClickListener listener)” upon ProgressDialog class object .

Show :- To show the dialog we call “Show()” upon  ProgressDialog object .

PD.show();

Dismiss  – To dismiss dialog we call “Dismiss()” upon ProgressDialog object .

PD.dismiss();

Lets See An Example

Project Detail

Project Name ProgressdialogDemo
Package com.pavan.progressdialogdemo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

 file:-progress_dialog.xml

<RelativeLayout
      xmlns:android=”http://schemas.android.com/apk/res/android”
      xmlns:tools=”http://schemas.android.com/tools”
      android:layout_height=”match_parent”
      android:layout_width=”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=”.ProgressDialogDemo” >

           <Button
                   android:id=”@+id/progess_but_id”
                   android:layout_alignparenttop=”true”
                   android:layout_centerhorizontal=”true”
                   android:layout_height=”wrap_content”
                   android:layout_margintop=”27dp”
                   android:layout_width=”wrap_content”
                   android:text=”@string/progress1″ />
    
</RelativeLayout>

2. Activity

file:-ProgressDialogDemo.java

package com.pavan.progressdialogdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ProgressDialogDemo extends Activity {

 Button progress_button;
 ProgressDialog pro_dialog;
 Handler pro_handler;
 int progress;
 private static final int MAX_PROGRESS = 100;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.progress_dialog);

  // intiliazing the buttons
  progress_button = (Button) findViewById(R.id.progess_but_id);

  progress_button.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // Creating progress dialog interface setting
    // title,progressstyle,max_progress
    pro_dialog = new ProgressDialog(ProgressDialogDemo.this);
    pro_dialog.setTitle(“Title”);
    pro_dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pro_dialog.setMax(MAX_PROGRESS);

    // adding positive button
    pro_dialog.setButton(DialogInterface.BUTTON_POSITIVE, “Hide”,
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int whichButton) {

        //  do some stuff on click
      }
     });

    // adding negative button
    pro_dialog.setButton(DialogInterface.BUTTON_NEGATIVE, “cancel”,
      new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog,
         int whichButton) {

        //do some stuff on click
      }
    });

    progress = 0;
    pro_dialog.show();
    pro_dialog.setProgress(0);
    pro_handler.sendEmptyMessage(0);

   }
  });

  // set onclick listener for buttons

  pro_handler = new Handler() {
   @Override
   public void handleMessage(Message msg) {
    // TODO Auto-generated method stub
    super.handleMessage(msg);
    if (progress >= MAX_PROGRESS) {
     pro_dialog.dismiss();
    } else {
     progress++;
     pro_dialog.incrementProgressBy(1);
     pro_handler.sendEmptyMessageDelayed(0, 100);
    }

   }
  };
 }
}



  • Hey Pavan, Thanks a lot for sharing this tutorial. We have some more advanced tutorials at http://www.fireboxtraining.com/java please do check it out.

  • on a long running process , where you can make the activity to be in invisible state by showing progress dialog until it completes the ongoing task

  • you welcome glad it helped you

  • Anonymous

    hi
    where to put long progress task?

  • Anonymous

    Hi there, thanks a lot for the tutorial.
    At first I couldn't get my ProgressDialog do anything but then I saw your code with "incrementProgressBy(1)" and it works fine =)
    Greets, T_D