comment 0

Tutorial : Build Simple Mobile Application :)

Ok , i want share about how to build a simple mobile application with java programming. It`s just a simple code which the result is only main form, and then you can select the option to execute next form, the second form is a registration form, and the last form it`s just show the result of what we fill in the second form, ok here we go :

1. I`m using a netbeans 6.9 to build this simple application, the netbeans 6.9 need java me to run the application mobile, first step is create a new project.
File –> New Project –> choose Java ME and select mobile application

click next ,don`t  forget to uncheck create hello midlet

Click finish

2. Create a new MIDlet in your packages

Give a name for a new MIDlet

3. Start to coding, ok i`ll not tell you about what the function of initialization , object, etc its not a basic of java , just about simple way to build simple mobile application

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.*;
 
/**
 * @author r3m1ck kuzmanovic
* I`m member of INDONESIANCODER
* contact me on r3m1ck@exploit-id.com
 */
public class simpleapp extends MIDlet implements CommandListener {
    private Display display;
 
    private List list;
    private Command cmdKeluar;
    private Command cmdPilih;
    private Ticker ticker1;
 
    private Form formRegistrasi;
    private TextField txtNama;
    private TextField txtTglLahir;
    private TextField txtJurusan;
    private Command cmdMenu;
    private Command cmdSubmit;
    private Ticker ticker2;
 
    private TextBox formBantuan;
    private Ticker ticker3;
 
    private Form formHasil;
    private StringItem itemNama;
    private StringItem itemTglLahir;
    private StringItem itemJurusan;
    private Ticker ticker4;
 
    private Alert alert;
 
    public simpleapp() {
        display = Display.getDisplay(this);
 
        list = new List("MENU UTAMA", List.IMPLICIT);
        list.append("Registrasi", null);
        list.append("Bantuan", null);
        ticker1 = new Ticker("Universitas Tertutup");
        cmdKeluar = new Command("Keluar", Command.EXIT, 1);
        cmdPilih = new Command("Pilih", Command.SCREEN, 2);
        list.addCommand(cmdKeluar);
        list.addCommand(cmdPilih);
        list.setTicker(ticker1);
        list.setCommandListener(this);
 
        formRegistrasi = new Form("Registrasi");
        txtNama = new TextField("Nama", "", 50, TextField.ANY);
        txtTglLahir = new TextField("Tgl Lahir", "", 50, TextField.ANY);
        txtJurusan = new TextField("Jurusan", "", 50, TextField.ANY);
        ticker2 = new Ticker("Registrasi");
        formRegistrasi.setTicker(ticker2);
        formRegistrasi.append(txtNama);
        formRegistrasi.append(txtTglLahir);
        formRegistrasi.append(txtJurusan);
        cmdMenu = new Command("Menu", Command.SCREEN, 1);
        cmdSubmit = new Command("Submit", Command.SCREEN, 2);
        formRegistrasi.addCommand(cmdMenu);
        formRegistrasi.addCommand(cmdSubmit);
        formRegistrasi.setCommandListener(this);
 
        formBantuan = new TextBox("Bantuan", "creator : r3m1ck kuzmanovic", 40, TextField.ANY);
        ticker3 = new Ticker("Bantuan");
        formBantuan.setTicker(ticker3);
        formBantuan.addCommand(cmdMenu);
        formBantuan.setCommandListener(this);
 
        formHasil = new Form("Hasil");
        itemNama = new StringItem("Nama : ", null);
        itemTglLahir = new StringItem("Tgl Lahir : ", null);
        itemJurusan = new StringItem("Jurusan : ", null);
        ticker4 = new Ticker("Hasil");
        formHasil.setTicker(ticker4);
        formHasil.append(itemNama);
        formHasil.append(itemTglLahir);
        formHasil.append(itemJurusan);
        formHasil.addCommand(cmdMenu);
        formHasil.setCommandListener(this);
 
        alert = new Alert("Error Form");
        alert.setTimeout(2500);
    }
 
    public void startApp() {
        display.setCurrent(list);
    }
 
    public void pauseApp() {
    }
 
    public void destroyApp(boolean unconditional) {
    }
 
    public void commandAction(Command c, Displayable d) {
        if (c == cmdKeluar) {
            destroyApp(false);
            notifyDestroyed();
        } else if (c == cmdPilih) {
            if (list.getString(list.getSelectedIndex()).equals("Registrasi")) {
                display.setCurrent(formRegistrasi);
            } else if (list.getString(list.getSelectedIndex()).equals("Bantuan")) {
                display.setCurrent(formBantuan);
            }
        } else if (c == cmdMenu) {
            display.setCurrent(list);
        } else if (c == cmdSubmit) {
            String PESAN_ERROR = "";
 
            if (txtNama.getString().trim().equals(""))
                PESAN_ERROR += " NAMA ERROR ";
            if (txtTglLahir.getString().trim().equals(""))
                PESAN_ERROR += " TGL LAHIR ERROR ";
            if (txtJurusan.getString().trim().equals(""))
                PESAN_ERROR += " JURUSAN ERROR ";
 
            if (PESAN_ERROR.equals("")) {
                itemNama.setText(txtNama.getString());
                itemTglLahir.setText(txtTglLahir.getString());
                itemJurusan.setText(txtJurusan.getString());
                display.setCurrent(formHasil);
            } else {
                alert.setString(PESAN_ERROR);
                display.setCurrent(alert);
            }
        }
    }
}


Sponsored links



4. Last way is just to run your mobile application with emulator, you can press F6 to build and run the application

Leave a Reply

Your email address will not be published. Required fields are marked *