Sunday 9 June 2013

Nokia Mobile Phone Application Development Stage 4

Sample App for understanding LWUIT :

asha mobile application

We discussed about using what is LWUIT ,what are the components of LWUIT and how to implement LWUIT in our project .If you are new to this topic please go through previous post before reading this post.

So here we created a very small sample app for better understanding

Sample App:

We hope you finished steps upto implementation of LWUIT into your project already.If not please start from the previous post since this is a continuation post of previous one.

Step 8:(First 7 steps available in previous post Refer it) Your code may resemble the one below after adding your Java ME MIDlet file to your project


import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

public class TestClass extends MIDlet {
public TestClass() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}
protected void pauseApp() {
// TODO Auto-generated method stub

}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub

}
}

Here startApp() -This method works whenever the app is started.

destroyApp() -Whenever we exit from our app destroy app will work

So lets start a sample app in this post.

To initialize LWUIT we need to use Display.init(this) in startApp() then we have to create a form and then have to add our UI components to the form.

For your convenience and reference we mentioned every UI component and their syntax in a separate post Please refer it.


Sample Code:(For Calculating EMI)

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Display.init(this);
b1=new Button("EMI Calculator");
b2=new Button("Help");
b3=new Button("About");
b4=new Button("Exit");
Form f1=new Form("Welcome");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
f1.addComponent(b1);
f1.addComponent(b2);
f1.addComponent(b3);
f1.addComponent(b4);
f1.show();


}
public void actionPerformed(ActionEvent ae) { 
if(ae.getSource()==b1)
{
final Form f2=new Form("Calculate your EMI");
final Label l1=new Label("Enter principal amount");
final TextField t1=new TextField("");
t1.setConstraint(TextField.NUMERIC);
final Label l2=new Label("Enter Rate of Interest per annum");
final TextField t2=new TextField("");
t2.setConstraint(TextField.NUMERIC);
final Label l3=new Label("Enter no of months");
final TextField t3=new TextField("");
t3.setConstraint(TextField.NUMERIC);
final Button b11=new Button("Calculate");
Command back=new Command("Back"){
public void actionPerformed(ActionEvent evt) {
                 // do something
try {
startApp();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
           }
      };


f2.addCommand(back);
f2.setBackCommand(back);
b11.addActionListener(new ActionListener() 
{
public void actionPerformed(ActionEvent evt)
{

if(t1.getText()=="" || t2.getText()=="" || t3.getText()=="")
{
Label err=new Label("Enter valid Input");
validDialog = new Dialog("Your EMI");
validDialog.setScrollable(false);
validDialog.setTimeout(2000);
validDialog.addComponent(err);

validDialog.showDialog();
}
else
{
double a=Double.parseDouble(t1.getText());
double b=Double.parseDouble(t2.getText());
double c=Double.parseDouble(t3.getText());
double f=b/1200;
double d=pow2(f+1,c);
double e=a*f*d/(d-1);
Label answer2=new Label(""+e);
al=new Alert(""+e);
 validDialog = new Dialog("Your EMI");
validDialog.setScrollable(false);
validDialog.setTimeout(5000);
validDialog.addComponent(answer2);

validDialog.showDialog();
}
}
private double pow2(double d, double c) {
// TODO Auto-generated method stub
double total=1;
   for(int i=1;i<=c;i++){
       total*=d;
   }
   return total;
}
});
f2.addComponent(l1);
f2.addComponent(t1);
f2.addComponent(l2);
f2.addComponent(t2);
f2.addComponent(l3);
f2.addComponent(t3);
f2.addComponent(b11);
f2.show();
}
if(ae.getSource()==b2)
{
Form f3=new Form("HELP");
Label a,c,d,e,f,g;
a=new Label("EMI CALCULATION");
c=new Label("Formula ");
d=new Label("E = P×r×(1 + r)n/((1 + r)n - 1)");
e=new Label("P -> Principal");
f=new Label("r -> Rate of Interest");
g=new Label("n -> No. of Months");
Command back=new Command("Back"){
public void actionPerformed(ActionEvent evt) {
               // do something
try {
startApp();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
         }
    };
       f3.addCommand(back);
       f3.setBackCommand(back);
f3.addComponent(a);
f3.addComponent(c);
f3.addComponent(d);
f3.addComponent(e);
f3.addComponent(f);
f3.addComponent(g);
f3.show();
}
if(ae.getSource()==b3)
{
Form f3=new Form("About");
Label a,b,c,d;
d=new Label("Developed by");
a=new Label("Digital Native Team");
b=new Label("App name: EMI Calculator");
c=new Label("Version Number:1.0.0");
f3.addComponent(d);
f3.addComponent(a);
f3.addComponent(b);
f3.addComponent(c);

Command back=new Command("Back"){
public void actionPerformed(ActionEvent evt) {
                // do something
try {
startApp();
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
          }
     };
  f3.addCommand(back);
f3.setBackCommand(back);
        f3.show();
}
if(ae.getSource()==b4)
{
notifyDestroyed();
}
}


}







3 comments :

  1. I would like to thank you for sharing this great information with us. I am really glad to learn about this because it helps me to increase my knowledge.Web Designing Companies Bangalore | Web Development Company

    ReplyDelete
  2. Hello, I’m really happy to say it was an interesting post to read.
    I learned new information from your article, you are doing a great job.
    Continue. web development bhopal

    website development in bhopal
    web development company in bhopal
    web design company in bhopal
    Seo Company in Bhopal

    ReplyDelete
  3. Nokia Mobile phones are elegant phones with great style and sophistication and a must have for those who want to make a style statement. The Nokia models are designed to suit your communication as well as entertainment needs.

    Mobile App Development

    ReplyDelete