Tuesday, March 16, 2010

J2ME - Alert - Code Examle

Alert is a J2ME way of showing messages, errors and prompts on screen. The exact look and feel will be different for each device but the purpose is same for all alerts across different J2ME phones. You can show text or image on the prompt and the error can either remain on the screen for a few seconds or may remain there forever, till the user presses ok and terminates the Alert.

How to show an Alert

Here is the code that shows the alert on the screen.

        Alert a = new Alert("This is the alert!");
        a.setTimeout(4000);
        display.setCurrent(a);

This Alert example will instantiate an Alert with the alert text message. The alert will be shown for 4 seconds (4000 miliseconds) and the last line is actually displaying the alert on the screen. User can hit ok or done while alert is shown to dismis the alert and go back to the screen it came from. By default the time-out is two seconds. So if you do not set the timeout, the alert will be shown for two seconds.

        Alert a = new Alert("This is the alert!");
        display.setCurrent(a);

If you want the Alert to be visible indefinitely, you can do it by using following piece of code:

        Alert a = new Alert("This is the alert!");
        a.setTimeout(Alert.FOREVER);
        display.setCurrent(a);
       
 This Alert can only be dismissed manually by pressing OK or Dismiss option on your phone. The exact label depends on the implementation on that particular phone.

How Alert is different
Alert is different from other types of J2ME screens. It keeps a portion of the previous screen visible and shows the error on top of it.

Output



In this tutorial, we displayed how to show a simple Alert using J2ME. Later, we will also cover a few different variants of the Alerts.



Related Articles

How to show Images in Alerts
Different Alert types and their implementation
J2ME - Form - Code Example


 

No comments:

Post a Comment