Professional Documents
Culture Documents
Mobile User Interface Design in Java
Mobile User Interface Design in Java
Mobile User Interface Design in Java
Java
MIT / Nokia Mobile Web Curriculum
Taken from Vidya Setlur’s Course on Java
Lecture 10
Goals for this Class
Gauges
Canvas Manipulation
Buttons
Icons
MIDP UI Packages
Canvas Graphics
Displaying a UI Component
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Choice;
List printList1;
List printList2;
public printerMidlet() {
Choice.MULTIPLE);
printList1.append(“Matrix", null);
printList1.append(“Crash", null);
printList1.append(“Incredibles", null);
printList2 = new List( "Select a movie you like - List 2", Choice.IMPLICIT, movies, null);
}
public void startApp() {
Display display = Display.getDisplay(this);
display.setCurrent(printList1);
try {
Thread.currentThread().sleep(10000);
}
catch(Exception e) {}
display.setCurrent(printList2);
}
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Display;
public printerMidlet() {
TextField.ANY);
Display.getDisplay(this).setCurrent(textBox);
}
In Class Exercise
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Choice;
public printerMidlet() {
form = new Form("Your Booking Details");
// a StringItem is not editable
stringItem = new StringItem("Your Id: ", “123456");
form.append(stringItem);
// you can accept Date, Time or DateTime formats
dateField = new DateField("Your Date of Arrival: ",
DateField.DATE);
form.append(dateField);
// similar to using a TextBox
txtField = new TextField( "Your Name: ", "", 60,
TextField.ANY);
form.append(txtField);
// similar to using a List
choiceGroup = new ChoiceGroup( “Room: ",
Choice.EXCLUSIVE, new String[] {“Smoking", "Non-
smoking"}, null);
form.append(choiceGroup);
// put some space between the items to segregate
spacer = new Spacer(20, 20);
form.append(spacer);
// a gauge is used to show progress
gauge = new Gauge("Step 1 of 3", false, 3, 1);
form.append(gauge);
// an image may not be found
try {
imageItem = new ImageItem( “Copyrighted by: ",
Image.createImage("/copyright.png"),ImageItem.LAYOUT_DEFAU
LT, “copyright");
form.append(imageItem);
}
catch(Exception e) {}
}
public void startApp() {
Display.getDisplay(this).setCurrent(form);
}
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Display;
public printerMidlet() {
Display.getDisplay(this).setCurrent(form);
}
Gauges
In Class Exercise
A MIDlet interacts with a user through commands and can only be associated with a
displayable UI element. Like a ticker, the Displayable class allows the user to attach a
command to it by using the method addCommand(Command command). Unlike a
ticker, a displayable UI element can have multiple commands associated with it.
The Command class holds the information about a command. This information is
encapsulated in four properties: a short label, an optional long label, a command
type, and a priority.
Command exitCommand = new Command("EXIT", Command.EXIT, 1);
Commands are immutable once created.
By specifying the type of a command, you can map any predefined keys on the
device to the command itself. For example, a command with the type OK will be
mapped to the device's OK key. Others are BACK, CANCEL, EXIT, HELP, ITEM, SCREEN,
and STOP. The SCREEN type relates to an application-defined command for the
current screen. Both SCREEN and ITEM usually never have any device-mapped keys.
By specifying a priority, you tell the AMS running the MIDlet where and how to show
the command. A lower value for the priority is of higher importance, and therefore
indicates a command that the user should be able to invoke directly. For example,
you would probably always have an exit command visible to the user and give it a
priority of 1. Since the screen space is limited, the device then bundles less-
important commands into a menu.
In order to provide interaction, the command is associated with a listener using the
method setCommandListener(CommandListener listener) from the Displayable
class.
The responsibility for acting on commands is performed by a class implementing the
CommandListener interface via the method: commandAction(Command cmd,
Displayable display).
In Class Exercise
public printerMidlet() {
canvas = new canvas();
}
public void startApp() {
Display display = Display.getDisplay(this);
display.setCurrent(canvas);
canvas.repaint();
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
}
Canvas (2)
class canvas extends Canvas {
public void paint(Graphics g) {
int width = getWidth();
int height = getHeight();
Image image= null;
try {
image = Image.createImage("/copyright.png");
}
catch(Exception e) {
e.printStackTrace();
}
g.setColor(0xffffff);
g.fillRect(0, 0, width, height);
g.drawImage(image, width/2,height/2 , Graphics.VCENTER |
Graphics.HCENTER);
}
}
Canvas (3)
In Class Exercise