I have already posted about the Alert Dialog and the Custom Dialog in Android. However, in this post, I will show you how to make your application fancier by adding Progress Dialog in it. What is a Progress Dialog? It is a dialog that shows the user how much of the current work, if any being performed, is complete. Below is an image that shows exactly what one looks like in an Eclair emulator.
Want to have one of these in your application? Open up an activity and start programming. Complete Source Code is at the bottom.
- Set the content view of the activity. Make sure the layout of the activity contains a button so that the Progress Dialog can be triggered on the click of the button.
- After setting the content view, declare and define the button in the java file.
- Add the Progress Dialog hereafter, using the following code.
[java]
final ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Working…");
pd.setIndeterminate(true);
pd.setCancelable(true);
[/java]We will not be defining the Progress Dialog inside the onClick() method of the button so that we can use this dialog in other places, if need be.
- Set up the onClickListener() and within it the onClick() methods. Inside the onClick() method, write the following code.
[java]
pd.show();
[/java]All we need to do is to show the dialog.
- Save your work and execute on an emulator/device.
Understanding the Code
Here I provide a little explanation as to what some of the functions in the declaration of the Progress Dialog are doing.
- setProgressStyle() : This defines the style in which the progress will be shown. This can take one of two values – Spinner or Horizontal. Both have their own set of values in the methods described below. I will discuss the Horizontal style in the next post.
- setMessage() : Quite obviously, it defines the message shown to the user while the Progress Dialog is displayed.
- setIndeterminate() : This defines whether the Progress Dialog is an ongoing percentage thing. It takes a boolean value as an argument. A value of true means that it will end once the work being done ends. At this time the dialog disappears. A value of false means we will be displaying details about the progress in percentage.
- setCancelable() : It tells the application whether the user can cancel the progress by pressing the back button. It again takes a boolean value as an argument.
COMPLETE SOURCE CODE
MainActivity.java
[java]
package com.nero.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.but);
final ProgressDialog pd = new ProgressDialog(Main.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Working…");
pd.setIndeterminate(true);
pd.setCancelable(true);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pd.show();
}
});
}
}
[/java]
Remember that the spinner in this dialog will keep spinning as we have not defined any action that needs to be completed. Hence you must hit the back button to end it.