Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

MIDP's User Interface Hierarchy: Alert

From Wiki
Jump to: navigation, search
Article Metadata
Created: thiagobrunoms (13 Oct 2009)
Last edited: hamishwillee (01 Aug 2013)

In our third part of the MIDP's User Interface Hierarchy series, we discussed about the List MIDP's UI component (MIDP's User Interface Hierarchy: List). In this fourth part I'll discuss about another Screen-based component called Alert (javax.microedition.lcdui.Alert).

The Alert is a screen-based component which aims to show a warning or an error message to the user. You can insert texts, images, and define the time interval that it should be displayed in the screen. The following table shows the main operations available in the Alert class.


An Alert screen can be devided into two classes: modal or time-based alert. The former is visible until the user dismiss it. The last one has a limited period of time to be displayed. The Alert screen can also has an Alert type associated to it. The AlertType (javax.microedition.lcdui.AlertType) class defines five types of alert (as described in the table above). Using AlertType, you can associate your alert event to a specific standard sound.

You can create your own alert by extending Alert and defining your own operations.

public class MyAlert extends Alert implements CommandListener {
//Constructor: creates an Alert screen with only a title
public MyAlert(String title) {
//Constructor: creates an Alert screen with a given title, text, image and type
public MyAlert(String title, String alertText, Image alertImage,
AlertType alertType) {
super(title, alertText, alertImage, alertType);
* Handle command events. If Alert.FOREVER is set as the time interval, the app can handle a dismiss command by checking arg0 == Alert.DISMISS_COMMAND
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)

public void commandAction(Command arg0, Displayable arg1) {
//Checks if the Alert screen was dismissed
if(arg0 == Alert.DISMISS_COMMAND) {
//Dismissing the Alert screen.
//"Display.setCurrent(newScreen)" can be used to setup a new screen

The following steps can be used to create a new MyAlert Alert screen and sets it to be visible

Display d = Display.getDisplay(midletReference);
//Creating an alert without images.
MyAlert alert = new MyAlert("MyAlert Title", "Alerting a user!", null, AlertType.CONFIRMATION);
//Setting its time interval to FOREVER. That is, a user must dismiss it.
//Sets "alert" to visible. After dismissed, list screen will be visible
d.setCurrent(alert, list);

If you wish to create an alert that shows progressive bars (as shown in the figure below), you can use the alert.setIndicator(gauge-item-component-reference) to it. The Gauge UI component will be discussed in later in the MIDP's User Interface article series.

//Sets the progressive bar to our MyAlert reference. Gauge component can be used to this goal.
alert.setIndicator(new Gauge(null, false, 10, 3));


This fourth part of this series introduced the Alert screen-based UI component. The next part I will talk over about the Form Screen-based component (MIDP's User Interface Hierarchy: Form).

This page was last modified on 1 August 2013, at 06:49.
46 page views in the last 30 days.

Was this page helpful?

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


Thank you!

We appreciate your feedback.