ProgressDialog

ProgressDialog is a Nokia Asha software platform extension to the standard Dialog component. It allows the construction of more complex Dialogs that can be used for displaying the progress of ongoing determinate or indeterminate tasks.

Indeterminate ProgressDialog with a Spinner, info text and a Button

An indeterminate ProgressDialog with a Spinner, can be used to display a short informative text and allow the user to cancel the ongoing operation. The cancel implementation is built-in, so that the developer does not have to assign an action listener. The ProgressDialog can also be dismissed by using the dispose method, which forces the MIDlet to back step, i.e. to return to the last displayable instance from where the Dialog was launched.

Figure 1. Indeterminate ProgressDialog with info text and a Button

String message = "Connecting to service";
Command cmdOk = new Command("Cancel");
progressDialog = new ProgressDialog(message, cmdOk);
progressDialog.show();

Indeterminate ProgressDialog with a Spinner and info text

The indeterminate ProgressDialog with a Spinner and informative text dismisses by itself once the operation is complete. The dismiss operation is not built-in and the developer needs to properly wrap the background operations in a thread, at the end of which the dispose method is called as shown below.

Figure 2. Indeterminate ProgressDialog with a Spinner and info text

progressDialog = new ProgressDialog("downloading");
thread = new Thread(new Runnable(){
	public void run() {
		try {
			// run for 3 seconds
			Thread.sleep(3000);
		}
		catch (InterruptedException e) {
			System.out.println("Interrupted exception:" + e.getMessage());
		}
		progressDialog.dispose();
	}
});
thread.start();
progressDialog.show();

Indeterminate ProcessDialog with an indeterminate ProgressBar, info text and a Button

This is a similar implementation as the Indeterminate ProgressDialog with a Spinner, info text and a Button, except in place of the Spinner, an indeterminate ProgressBar is used. The ProgressBar needs to be initialized and passed as an argument to the ProgressDialog constructor. The dismiss operation is built-in.

Figure 3. Indeterminate ProgressDialog with ProgressBar, info text and a Button

ProgressBar bar = new ProgressBar(true);
progressDialog = new ProgressDialog("Downloading", bar, "Please wait", cancel);
progressDialog.show();

Determinate ProgressDialog with a determinate ProgressBar, info text and a Button

This ProgressDialog is capable of updating its title, to display for example the completion of the ongoing task as a percentage or allow the end user to cancel the task entirely. The title is fully customizable.

Figure 4. Determinate ProgressDialog with ProgressBar, info text and a Button

It is advised to keep the implementation of the background Thread in a separate class. This will create and show the ProgressDialog:

ProgressBar bar = new ProgressBar("", false);
Command cancel = new Command("Cancel");
ProgressDialog progressDialog = new ProgressDialog("", bar, "Downloading", cancel);
Thread thread = new Thread(new ProgressDialogRunnable(progressDialog));
thread.start(); 
progressDialog.show();

The code for the class that implements the Runnable interface needs to access the ProgressDialog instance passed to it from the class where the Thread is initiated and the title is updated using the setTitle method as follows:

public class ProgressDialogRunnable 
    implements Runnable {

    private ProgressDialog progressDialog;
    
    ProgressDialogRunnable(ProgressDialog progressDialog) {
        this.progressDialog = progressDialog;
    }
    
    public void run() {
        for(int i = 1; i <= 10; i++){
            try {
                int percent = i * 10;
                progressDialog.setProgressValue(percent);
                progressDialog.setTitle(percent + "%");                           
                Thread.sleep(1000);
            }  
            catch (InterruptedException e) {
                System.out.println("Interrupted exception:" + e.getMessage());
            }
        }
        progressDialog.dispose();
    }
}
 

Last updated 4 December 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×