ProgressDialog:
Android ProgressDialog is a dialog box/dialog window which shows the progress of a task. The progress dialog will be used if the we want the user to wait till the task complete. This is a extension of AlertDialog. (To know more about AlertDialog in Andrid, visit this page AlertDialog example). ProgressDialog is almost same as ProgressBar with the exception that this is displayed as a dialog box. This will display the progress in numbers.Difference between ProgressBar and ProgressDialog:
The difference between ProgressBar and ProgressDialog is given in the another tutorial. View it now.
Important things about ProgressDialog:
setMessage(); method is used to show the message to the user. Example: Loading... (or) Please wait....setTitle(); method is used to set a title to the dialog box.
setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); is used to show the horizontal progress bar in the dialog box.
setProgressStyle(ProgressDialog.STYLE_SPINNER); is used to show the circle/spinning progress bar in the dialog box.
setMax(); method is used to set the maximum value.
getProgress(); method is used to get the current progress value in numbers.
How to create:
ProgressDialog can be created programatically using the ProgressDialog class instance as shown below;ProgressDialog progressDoalog=new ProgressDialog(MainActivity.this);
Here MainActivity.this is the activity class.
Example program:
In this example program I am creating a ProgressDialog and increment its value by 1 using the progressDoalog.incrementProgressBy(1); method. handle.sendMessage(handle.obtainMessage()) activates the Handler in which we’ve written the code for incrementing the Progress bar.I made the thread to sleep for 200 milliseconds after every increment to show the progress slowly.
Android layout xml file: activity_mail.xml
<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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ProgressDialog example" />
</RelativeLayout>
Activity class: MainActivity.java:
package com.example.progressdialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Android ProgressDialog example
* @author Prabu Dhanapal
* @version 1.0
* @since SEP 01 2013
*
*/
public class MainActivity extends Activity {
Button button;
ProgressDialog progressDoalog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
progressDoalog = new ProgressDialog(MainActivity.this);
progressDoalog.setMax(100);
progressDoalog.setMessage("Its loading....");
progressDoalog.setTitle("ProgressDialog bar example");
progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDoalog.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
while (progressDoalog.getProgress() <= progressDoalog
.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDoalog.getProgress() == progressDoalog
.getMax()) {
progressDoalog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDoalog.incrementProgressBy(1);
}
};
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items
//to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
good
ReplyDelete