Android Progress Dialog

ProgressDialog is used to indicate the status of the progress.In addition to that it will show optional text message.This progressDialog can be cancelled on back key press.The Progress changes from 0..10000.

Some task like uploading or downloading a file takes more time to complete. Incase of this ProgressDialog will show the progress of the task.

ProgressDialog has two type of styles:

progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) This line is used to show the Horizontal style.

progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER) This line is used to show the Spinner style.

You can download source code here.

This is ProgressDialog Code:

package com.etr.progressdialogdemo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ProgressDialogActivity extends Activity {
ProgressDialog progressDialog;
Handler progressHandler;
int count;
Button start;

@Override
protected void onCreate(Bundle savedInstanceState) {

try {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_dialog);

progressHandler = new Handler();

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

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
progress();
}
});

} catch (Exception e) {

Log.v("Exception", Log.getStackTraceString(e));

}
}

public void progress() {

progressDialog = new ProgressDialog(this);
progressDialog.setTitle("ProgressDialog ...");
progressDialog.setMessage("Download/Upload in progress ...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(0);
progressDialog.setMax(100);
progressDialog.show();
new Thread(new Runnable() {
@Override
public void run() {

try {

while (progressDialog.getProgress() <= progressDialog
.getMax()) {

Thread.sleep(2000);
progressHandler.post(new Runnable() {

public void run() {

progressDialog.incrementProgressBy(5);

}

});

if (progressDialog.getProgress() == progressDialog
.getMax()) {

progressDialog.dismiss();
       break;

}

}

} catch (Exception e) {

}

}

}).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.progress_dialog, menu);
return true;
}

}

This is code for layout file :

<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=".ProgressDialogActivity" >

    <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="78dp"
        android:text="Start ProgressDialog" />

</RelativeLayout>

Output :

Comments

  1. Hi,
    Your source code doesn't work...even my source code that I got from you doesn't work

    public void saveFileProg() {

    pd = new ProgressDialog(this);
    pd.setTitle("Saving File...");
    //pd.setMessage("Wait...");
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setCanceledOnTouchOutside(false);
    pd.setIndeterminate(true);
    pd.setProgress(0);
    pd.setMax(100);
    pd.show();
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {

    while (pd.getProgress() <= pd
    .getMax()) {
    Thread.sleep(500);
    ph.post(new Runnable() {
    public void run() {
    pd.incrementProgressBy(10);
    fileSave();
    }
    });
    if (pd.getProgress() == pd
    .getMax()) {
    pd.dismiss();
    break;
    }
    }
    } catch (Exception e) {
    //Toast.makeText(getApplicationContext(), "Error..." ,Toast.LENGTH_LONG);
    }
    }
    }).start();
    }

    Ron,

    ReplyDelete

Post a Comment

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService