×
Namespaces

Variants
Actions
(Difference between revisions)

How to add an activity or progress indicator to an Alert

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Minor subedit for wiki style)
chintandave_er (Talk | contribs)
(Chintandave er - code formatting)
Line 47: Line 47:
  
 
<code java>
 
<code java>
            Form mainForm;
+
Form mainForm;
//OK and Exit are added to the Form
+
Command exitCommand = new Command("Exit", Command.EXIT, 0);
+
Command okCommand = new Command("OK", Command.OK, 0);
+
//Cancel is added to the Alert
+
Command cancelCommand = new Command("Cancel", Command.CANCEL, 0);
+
//A 1 second per step thread
+
Thread thread;
+
Alert alert;
+
//A random image for the alert
+
Image img;
+
//A bar indicator attached to the alert
+
Gauge indicator;
+
  
 +
//OK and Exit are added to the Form
 +
Command exitCommand = new Command("Exit", Command.EXIT, 0);
 +
Command okCommand = new Command("OK", Command.OK, 0);
 +
 +
//Cancel is added to the Alert
 +
Command cancelCommand = new Command("Cancel", Command.CANCEL, 0);
 +
 +
//A 1 second per step thread
 +
Thread thread;
 +
Alert alert;
 +
 +
//A random image for the alert
 +
Image img;
 +
 +
//A bar indicator attached to the alert
 +
Gauge indicator;
 
 
protected void startApp() throws MIDletStateChangeException {
+
protected void startApp() throws MIDletStateChangeException {
//The Form's components
+
//The Form's components
mainForm = new Form("Progress Alert");
+
mainForm = new Form("Progress Alert");
mainForm.append("Select OK to launch a 10-step process");
+
mainForm.append("Select OK to launch a 10-step process");
mainForm.addCommand(okCommand);
+
mainForm.addCommand(okCommand);
mainForm.addCommand(exitCommand);
+
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
+
mainForm.setCommandListener(this);
display = Display.getDisplay(this);
+
display = Display.getDisplay(this);
display.setCurrent(mainForm);
+
display.setCurrent(mainForm);
 
</code>
 
</code>
  
Line 87: Line 91:
  
 
<code java>
 
<code java>
        indicator = new Gauge(null, false, 100, 0);
+
indicator = new Gauge(null, false, 100, 0);
        alert.setTimeout(Alert.FOREVER);         
+
alert.setTimeout(Alert.FOREVER);         
        alert.setCommandListener(this);   
+
alert.setCommandListener(this);   
        alert.setIndicator(indicator);
+
alert.setIndicator(indicator);
 
</code>
 
</code>
  
Line 100: Line 104:
  
 
<code java>
 
<code java>
public void run() {
+
public void run() {
try {
+
      try {
int current = indicator.getValue();
+
int current = indicator.getValue();
//A ten step increment from 0 to 100 in steps of 10
+
//A ten step increment from 0 to 100 in steps of 10
while(current < 100){
+
while(current < 100){
current = indicator.getValue();
+
        current = indicator.getValue();
indicator.setValue(current + 10);
+
indicator.setValue(current + 10);
//One second pause between the steps
+
//One second pause between the steps
Thread.sleep(1000);
+
Thread.sleep(1000);
}
+
                }
} catch (InterruptedException e) {
+
        }
//Dismisses the alert by interrupting the progress thread
+
        catch (InterruptedException e) {
e.printStackTrace();
+
//Dismisses the alert by interrupting the progress thread
}
+
e.printStackTrace();
//Displays the original screen and resets the progress bar value
+
display.setCurrent(mainForm);
+
indicator.setValue(0);
+
+
 
}
 
}
 +
//Displays the original screen and resets the progress bar value
 +
display.setCurrent(mainForm);
 +
indicator.setValue(0);
 +
}
 
</code>
 
</code>
  
Line 124: Line 128:
  
 
<code java>
 
<code java>
public void commandAction(Command c, Displayable arg1) {
+
public void commandAction(Command c, Displayable arg1) {
if(c == cancelCommand) {
+
if(c == cancelCommand) {
//Interrupts the thread that moves the progress bar
+
//Interrupts the thread that moves the progress bar
thread.interrupt();
+
thread.interrupt();
}
+
}
 +
}
 
</code>
 
</code>
  

Revision as of 09:04, 25 July 2012

This code example demonstrates how to add an activity indicator to an Alert, that displays the progress for the completion of a process and dismisses itself, when the process is completed.

Contents

Overview

This code example uses two high-level LCDUI components, an Alert and a Gauge. The Alert is manually launched by the end user, while the Gauge is used as activity indicator inside the Alert. The activity indicator describes the progress for completing a 10-step task. The range of the indicator is between 0 and 100, in increments of 10.

Main Components

There are three Commands in this code example. An OK command that launches the Alert and an Exit Command for calling the notifyDestroyed() method, are appended to the Form. A Cancel Command is added to the Alert. The running thread moves the progress indicator every one second in increments of 10. There is also an image that is being displayed inside the alert.

Note.pngNote: In order for the image to be properly displayed with this code, you need to add an image file to the resource directory of your project and rename it to generic_phone.png.

 Form mainForm;
 
//OK and Exit are added to the Form
Command exitCommand = new Command("Exit", Command.EXIT, 0);
Command okCommand = new Command("OK", Command.OK, 0);
 
//Cancel is added to the Alert
Command cancelCommand = new Command("Cancel", Command.CANCEL, 0);
 
//A 1 second per step thread
Thread thread;
Alert alert;
 
//A random image for the alert
Image img;
 
//A bar indicator attached to the alert
Gauge indicator;
 
protected void startApp() throws MIDletStateChangeException {
//The Form's components
mainForm = new Form("Progress Alert");
mainForm.append("Select OK to launch a 10-step process");
mainForm.addCommand(okCommand);
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(mainForm);

Conditions for adding a Gauge as activity indicator within an Alert

Only under certain conditions, can a high-level Gauge be used within an Alert as activity indicator. The Gauge needs to satisfy the following conditions:

  • it must be non-interactive
  • it must not be owned by another container (Alert or Form)
  • it must not have any Commands
  • it must not have an ItemCommandListener
  • it must not have a label (that is, its label must be null
  • its preferred width and height must both be unlocked, and
  • its layout value must be LAYOUT_DEFAULT.

This practically means that we need to instantiate the Gauge object with null for the first argument that denotes the label and false for the second argument that denotes the boolean that determines whether the Gauge is interactive or not:

indicator = new Gauge(null, false, 100, 0);
alert.setTimeout(Alert.FOREVER);
alert.setCommandListener(this);
alert.setIndicator(indicator);

As seen from the code above, the alert uses a CommandListener for canceling the activity. That requires that we set the Timeout to FOREVER so that the end user can only dismiss the alert by clicking on the Cancel button rather than by tapping anywhere on the screen.

The thread that simulates the progress of the activity

The indicator in this example can be used in connection with measuring the progress of a task with definite range. This means that we know in advance how many steps the task consists of. As examples of tasks with definite ranges can be considered downloading a file whose size is known. The example can be adjusted to the measuring quantity. Here, we assume that a task's range is from 0 up to 100 and consists of 10 steps. The thread pauses for 1 second then increments the value of the indicator by 10 until it reaches 100. At that point, the application exits the while loop and dismisses the Alert as shown below:

public void run() {
try {
int current = indicator.getValue();
//A ten step increment from 0 to 100 in steps of 10
while(current < 100){
current = indicator.getValue();
indicator.setValue(current + 10);
//One second pause between the steps
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
//Dismisses the alert by interrupting the progress thread
e.printStackTrace();
}
//Displays the original screen and resets the progress bar value
display.setCurrent(mainForm);
indicator.setValue(0);
}

The alert can also be dismissed by clicking on the Cancel button. The code inside this action, simply interrupts the thread:

public void commandAction(Command c, Displayable arg1) {
if(c == cancelCommand) {
//Interrupts the thread that moves the progress bar
thread.interrupt();
}
}

See also

84 page views in the last 30 days.
×