Displaying Messages in Alert using LWUIT for Nokia
Displaying Alert in LWUIT for Nokia
When using LWUIT we should first add and initialise LWUIT in our project.If you want to know how to add LWUIT and Initialise LWUIT please read our previous post NOKIA MOBILE APPLICATION DEVELOPMENT STAGE 3 first then only you can use the following tutorial.
Alert in LWUIT is somewhat different from Alert in LCUDI.We have Alert class in LCUDI which can be directly used in our project instead of that we have to Dialog Class in LWUIT
To create an Alert in LWUIT:
Dialog alert=new Dialog();
alert.setTitle("Alert Message");
You can also set timeout for each alert message in LWUIT like in LCUDI but the time must in Microsecond format.
To set timeOut for Alert in LWUIT:
alert.setTimeout(2000);
To display the alert in screen you can use two different ways one is using show() method another one is to use showDialog() method.
show() method:
show(int,int,int,int,boolean) is the syntax for show method in which you have to mention the position of the Alert message and final argument boolean is used to mention whether to include the Title for Alert or not (True for showing Title and False for not showing)
e.g
alert.Show(90,90,10,10,true);
showDialog() method:
ShowDialog() doesn't accept any parameters but when you use the showDialog() method your alert message will be in centre of screen by default.
e.g
alert.showDialog();
SAMPLE CODE:
import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;
import com.sun.lwuit.Button;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
public class TestClass extends MIDlet {
Form f1;
Button b1;
Dialog alert;
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
Display.init(this);
f1=new Form("LWUIT Alert");
b1=new Button("Show Alert");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
alert=new Dialog();
alert.setTitle("I am a Alert");
alert.setTimeout(2000);
alert.showDialog();
}
});
f1.addComponent(b1);
f1.show();
}
}
The output will resemble the one given below
No comments:
Post a Comment