Android Spinning Progress Dialog With AsyncTask

By | November 17, 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 create spinning progress dialog and put the long running process using asynctask . you can refer the asynctask tutorial which i covered in my previous post .

Android SpinnerView With AsyncTask


 Read  : ProgressBared ProgressDialog 

Constructing Progress Dialog

The Android SDK Provides ProgressDialog(Context context) class for creating progress dialog 

ProgressDialog PD =new new ProgressDialog( context );

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

 PD.setTitle(“Title”);     

Message :- To set the message we call “setMessage(CharSequence msg)” upon
ProgressDialog object .
PD.setMessage(“Messaage”);

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

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
    xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical”
    android:padding=”30dp” >

    <Button
        android:id=”@+id/button1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_gravity=”center”
        android:layout_marginTop=”20dp”
        android:text=”Show ProgressDialog” />

</LinearLayout>

2. Activity

package com.pavan.progressdialogdemo;

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

import com.example.progressdialogdemo.R;

public class MainActivity extends Activity {
 Button bt;
 ProgressDialog PD;

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

  bt = (Button) findViewById(R.id.button1);
  bt.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    new MyAsync().execute();
   }
  });
 }

 private class MyAsync extends AsyncTask {

  @Override
  protected void onPreExecute() {
 
   super.onPreExecute();
   PD = new ProgressDialog(MainActivity.this);
   PD.setTitle(“Please Wait..”);
   PD.setMessage(“Loading…”);
   PD.setCancelable(false);
   PD.show();
  }

  @Override
  protected Void doInBackground(Void… params) {
   try {
    Thread.sleep(3000);
   } catch (InterruptedException e) {
  
    e.printStackTrace();
   }
   return null;
  }

  @Override
  protected void onPostExecute(Void result) {  
   super.onPostExecute(result);
   PD.dismiss();
  }
 }
}