Sunday, April 11, 2010

Workshop 1 - Part 2 - HelloWorld for Blackberry

Creating a HelloWorld Blackberry application

Whenever we start to learn any programming language, we start with a HelloWorld application that prints the text "Hello World!" in the screen, browser or the console. So, in this tutorial, I will start by creating a HelloWorld application for Blackberry.


Creating a Blackberry project in Eclipse

To create a new Blackberry project

Click on File >> New >> Project
Choose Blackberry >> Blackberry Project as shown below



Click on Next and enter Project name as shown below.



Click on Next and the setting will appear like below.




The project is now set up. A screen will appear showing the "Blackberry Application Descriptor". I have added a title and a decription for the application as shown above.


Adding a new class:


To add a new class - Click on New and choose Class. A prompt will appear. Add a Package name and a class name as shown below and click Finish. The class will then be created.




UiApplication


Blackberry applications that have a user interface must extend the UiApplication class. A UI application contains a stack of screen objects. When it adds new screen on to the stack, it drwas them on top of other screens already on the stack. Only the screen on the top of the stack receives input.

To create a UiApplication, the HelloWorld class needs to extend the net.rim.device.api.ui.UiApplication class. The code will like below.




Now, I will add a main method to the class and initiate a new object of type HelloWorld. In the constructor, I will add a new Screen object and display it. The code is shown below.




MainScreen

net.rim.device.api.ui.container.MainScreen class provides a working acrea where data from HelloWorld application can be displayed. I will nowcreate the HelloWorldScreen class shown in the code previously and extend the MainScreen class.

In the constructor, I will add a net.rim.device.api.ui.component.LabelField to set the title of the application and add a net.rim.device.api.ui.component.RichTextField to display the "Hello World!" message. Then, I will add confirmation dialog that displays a message before closing the application. I will need to overwrite onClose() method for this. The code for this class is  below.





Running the application

Running the application is quite simple. Just click on the Debug button and the Blackberry simulator will start. Click on the Applications menu and then Donwloads menu. The HelloWorld application will appear as shown below.



Clicking on HelloWorld application will show the screen as shown below.



Trying to move to any other screen will show the dialog for "closing the application" as shown below.




1 comment:

  1. your are give full detail with image is very nice about blackberry. keep it up the more information about Mobile Application Development

    ReplyDelete