Progress Dialog in Android

  • To create the progress bar, first of all we have to initialize the PogressDialog with the class constructor such as ProgressDialog(context)
  • Now we have to set the style to STYLE_HORIZONTAL with setProgressStyle() and we can set other properties like message.
  • Now call show() method to display the progress dialog or return the ProgressDialog from onCreateDialog(int) callback.
  • We can increment the duration of progress which is displayed on the bar just by calling either setProgress(int) and pass value for the toatal percentage completed or we can use incrementProgressBy(int) and pass the value to add the total percentage to completed so far.
  • The xml layout file for this is shown below:
  • <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/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:onClick="download"
    android:text="Click to Download" />

    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="64dp"
    android:text="Eitworld.com"
    android:textColor="#ff16ff01"
    android:textSize="35dp" />

    </RelativeLayout>
  • In this layout file we have used the relative layout and use one button on which we have to perform the action and use one text view which displays the message “Eitworld.com”. The java file is shown below:
  • package com.example.progressdialogdemo;

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

    public class MainActivity extends Activity {

    Button b1;
    ProgressDialog progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1 = (Button) findViewById(R.id.button2);
    }

    public void download(View view){
    progress=new ProgressDialog(this);
    progress.setMessage("Downloading Android Notes");
    progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progress.setIndeterminate(true);
    progress.setProgress(0);
    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;
    }

    }
  • In this java file we have used the following packages :
  • android

  • Here we have used android.app.ProgressDialog package which provides the methods for creating progress bar.
  • Here we have a class MainActivity which extends Activity class as follows:
  • android

  • Now we have declared following variables:
  • android

  • Here we have declared two variables: b1 is type of Button and second is progress of type ProgressDialog.
  • Now we have to register the button inside the onCreate() method as follows:
  • android

  • Here we have registered the button using the id which is defined in the xml layout file.
  • Here we have made a method named download() as follows:
  • android

  • This method takes one parameter of type View.
  • In this method, first of all we have to create the constructor of ProgressDialog class and pass the context.
  • After that we have set the progress dialog message by using progress.setMessage(“Downloading Android Notes”); here we pass the message as the parameter.
  • After that we have set the style using the setProgressStyle() and pass the ProgressDialog.STYLE_HORIZONTAL as parameter.
  • After that we have to call the setInterminate(true) and pass the true as parameter.
  • After that we have to pass the progress in form of integer in the setProgress(0) method.
  • Now we have to call the progress.show() method.
  • After that we have declared one final type variable totalProgressTime and initialized with 100.
  • After that we have created Thread object of final type and used the anonyms method and inside this, implement the run() method.
  • Here we have used the above logic to show the progress bar in a flow.
  • The output is as follows:
  • android

    When app launched

    android

    when click on button