MOBILE APPLICATION DEVELOPMENT LAB
2011
GRIET
MOBILE APPLICATION DEVELOPMENT LAB MCA VTH SEMESTER
N V Ganapathi Raju Associate Professor,
Department of MCA Gokaraju Rangaraju Institute of Engineering & Technology
1
MOBILE APPLICATION DEVELOPMENT LAB
// 1. J2ME Hello World Midlet Program import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class HelloWorld extends MIDlet{ private Form form; private Display display; public HelloWorld(){ super(); } public void startApp(){ form = new Form("Hello World"); String msg = "Hello World!!!!!!!"; form.append(msg); display = Display.getDisplay(this); display.setCurrent(form); } public void pauseApp(){} public void destroyApp(boolean unconditional){ notifyDestroyed(); } }
N V Ganapathi Raju
MCA V SEMESTER
Page 2
MOBILE APPLICATION DEVELOPMENT LAB
// 2.Text Field Midlet Program import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class TextFieldExample extends MIDlet implements CommandListener{ private Form form; private Display display; private TextField name, company; private Command ok; public TextFieldExample(){ name = new TextField("Name:", "", 30, TextField.ANY); company = new TextField("Company Name:", "", 30, TextField.ANY); ok = new Command("OK", Command.OK, 2); } public void startApp(){ display = Display.getDisplay(this); Form form = new Form("Text Field"); form.append(name); form.append(company); form.addCommand(ok); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp(){ } public void destroyApp(boolean destroy){ notifyDestroyed(); } public void showInput(){ display = Display.getDisplay(this); String n = name.getString(); String c = company.getString(); Form form = new Form("Input Value"); form.append(n); form.append(c); display.setCurrent(form); }
public void commandAction(Command c, Displayable d) { String label = c.getLabel(); N V Ganapathi Raju
MCA V SEMESTER
Page 3
MOBILE APPLICATION DEVELOPMENT LAB
if(label.equals("OK")){ showInput(); } } }
N V Ganapathi Raju
MCA V SEMESTER
Page 4
MOBILE APPLICATION DEVELOPMENT LAB
// 3.Radio Button Program using ChoiceGroup import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class RadioButtons extends MIDlet implements ItemStateListener, CommandListener { private Display display; private Form form; private Command exit; private Item selection; private ChoiceGroup radioButtons; private int defaultIndex; private int radioButtonsIndex; public RadioButtons() { display = Display.getDisplay(this); radioButtons = new ChoiceGroup( "Select Your Color", Choice.EXCLUSIVE); radioButtons.append("Red", null); radioButtons.append("White", null); radioButtons.append("Blue", null); radioButtons.append("Green", null); defaultIndex = radioButtons.append("All", null); radioButtons.setSelectedIndex(defaultIndex, true); exit = new Command("Exit", Command.EXIT, 1); form = new Form(""); radioButtonsIndex = form.append(radioButtons); form.addCommand(exit); form.setCommandListener(this); form.setItemStateListener(this); } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command command, Displayable displayable) { if (command == exit) { destroyApp(true); N V Ganapathi Raju
MCA V SEMESTER
Page 5
MOBILE APPLICATION DEVELOPMENT LAB
notifyDestroyed(); } } public void itemStateChanged(Item item) { if (item == radioButtons) { StringItem msg = new StringItem("Your color is ", radioButtons.getString(radioButtons.getSelectedIndex())); form.append(msg); } } }
N V Ganapathi Raju
MCA V SEMESTER
Page 6
MOBILE APPLICATION DEVELOPMENT LAB
// 4 .CheckBox Button in J2ME using ChoiceGroup import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class CheckBoxExample extends MIDlet implements CommandListener { private Display display; private Form form; private Command exit, choose; private ChoiceGroup technology; private int index; public CheckBoxExample() { form = new Form("Technologies"); technology = new ChoiceGroup("Select Technology Which You Know", Choice.MULTIPLE); exit = new Command("Exit", Command.EXIT, 1); choose = new Command("Choose", Command.SCREEN, 2); } public void startApp() { display = Display.getDisplay(this); technology.append("JAVA", null); technology.append("J2ME", null); technology.append("J2EE", null); technology.append("JSF", null); index = form.append(technology); form.addCommand(exit); form.addCommand(choose); form.setCommandListener(this); display.setCurrent(form); } public void pauseApp(){} public void destroyApp(boolean unconditional){ notifyDestroyed(); } public void commandAction(Command c, Displayable displayable){ String label = c.getLabel(); if (label.equals("Choose")) { StringItem message[] = new StringItem[technology.size()]; boolean get[] = new boolean[technology.size()]; technology.getSelectedFlags(get); for (int i = 0; i < get.length; i++) { if (get[i]) { N V Ganapathi Raju
MCA V SEMESTER
Page 7
MOBILE APPLICATION DEVELOPMENT LAB
message[i] = new StringItem("Your Choice is: ", technology.getString(i)); form.append(message[i]); } } form.delete(index); form.removeCommand(choose); } else if (label.equals("Exit")){ destroyApp(false); } } }
N V Ganapathi Raju
MCA V SEMESTER
Page 8
MOBILE APPLICATION DEVELOPMENT LAB
// 5 Online help program using command buttons import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class MappingCommands extends MIDlet implements CommandListener { private Display display; private Form fmMain; private TextBox tbHelp; private Command cmExit; private Command cmHelp; private Command cmBack ; public MappingCommands() { display = Display.getDisplay(this); cmHelp = new Command("Help", Command.HELP, 1); cmBack = new Command("Back", Command.BACK, 1); cmExit = new Command("Exit", Command.EXIT, 1); fmMain = new Form("Core J2ME"); fmMain.addCommand(cmExit); fmMain.addCommand(cmHelp); fmMain.setCommandListener(this); tbHelp = new TextBox("Help", "Help text here...", 25, 0); tbHelp.addCommand(cmBack); tbHelp.setCommandListener(this); } public void startApp() { display.setCurrent(fmMain); } public void pauseApp() {} public void destroyApp(boolean unconditional) {}
public void commandAction(Command c, Displayable s) { N V Ganapathi Raju
MCA V SEMESTER
Page 9
MOBILE APPLICATION DEVELOPMENT LAB
if (c == cmExit) { destroyApp(false); notifyDestroyed(); } else if (c == cmHelp) display.setCurrent(tbHelp); else if (c == cmBack) display.setCurrent(fmMain); } }
N V Ganapathi Raju
MCA V SEMESTER
Page 10
MOBILE APPLICATION DEVELOPMENT LAB
// 6. List object cut copy paste import javax.microedition.midlet.MIDlet; import javax.microedition.lcdui.*; public class CutCopyList_h2 extends MIDlet implements CommandListener { private Form form; private String actions[]= {"cut", "copy", "paste", "delete", "select all", "unselect all"}; private List actionList; public CutCopyList_h2() { actionList = new List("Edit", List.EXCLUSIVE, actions, null); actionList.addCommand(new Command("Select list item", Command.SCREEN, 1)); actionList.setCommandListener(this); } public void startApp() { Display display; display = Display.getDisplay(this); display.setCurrent(actionList); } public void pauseApp() { } public void destroyApp(boolean unconditional) { notifyDestroyed(); } public void commandAction(Command c, Displayable d) { int i = actionList.getSelectedIndex(); String selectedItem = actionList.getString(i); Alert alert = new Alert("Your Selection"); alert.setString(selectedItem); alert.setTimeout(5000); // 5 seconds Display display = Display.getDisplay(this); display.setCurrent(alert, display.getCurrent() ); } }
N V Ganapathi Raju
MCA V SEMESTER
Page 11
MOBILE APPLICATION DEVELOPMENT LAB
N V Ganapathi Raju
MCA V SEMESTER
Page 12
MOBILE APPLICATION DEVELOPMENT LAB
// 7. Checking the phone number validation using Text box import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class VerifyPhonenumber_h4 extends MIDlet implements CommandListener { private Display display; private Form form; private Command cmdTest; private Command cmdExit; private TextField phoneNum; private String areaCodes[] = {"050", "040", "044", "041", "0400"}; public VerifyPhonenumber_h4() { display = Display.getDisplay(this); cmdTest = new Command("Check", Command.SCREEN, 1); cmdExit = new Command("Exit", Command.EXIT, 1); phoneNum = new TextField("Phone:", "", 11, TextField.PHONENUMBER); form = new Form("Insert the phone number"); form.addCommand(cmdExit); form.addCommand(cmdTest); form.append(phoneNum); form.setCommandListener(this); } public void startApp() { display.setCurrent(form); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == cmdTest) { if (phoneNum.size() >= 9 && phoneNum.size()