BlackBerry Application Development Tutorial (Part-3) Aasim Naseem, October 17, 2009 Hello All;I wish you receive this post in the best of everything.This is the 3rd part of BlackBerry Application Development Tutorial series. first of all, accept my apology for posting this post so late. I was busy in some other projects and hence couldn’t find enough time to compose it. I delayed this task for this weekend and now its read for you. Note: This tutorial is a part of serieis of my tutorials on blackberry application development; So far we have discussed the followings.Introduction :: BlackBerry Application DevelopmentSetting Up Your System :: BlackBerry Application Development Tutorial (Part-2)Your First Application :: BlackBerry Application Development Tutorial (Part-3)Creating A WebIcon :: BlackBerry Application Development Tutorial (Part-4)The pre-request of this tutorial is BlackBerry Application Development Tutorial (Part-2) for setting your system to start blackberry application development.Before start, I’m assuming that you have configure the Sun JDK, Eclipse SDK, BlackBerry JDE Plug-in for Eclipse and BlackBerry JDE Component Packs successfully on your system … is that? Kindly confirm again …Orite, its time to move forward. Bring a cup of coffee, some cookies, play some light song in your favorite music player …. I think some instrumental number will good …Hmm now you are ready to get start .. .good … start eclipse and make a new workspace for your blackberry projects.Orite, next is create a new project. As you have already install JDE Plug-in for eclipse, you will see a entry of “BlackBerry Project” in create new project window.Click next. Chose your project name, but for god sake, don’t use HelloWorld type world .… its time to say good by to Hello World … do something new dude … ummm use “WelcomeBlackBerry” .. hmm that’s nice …Chose default location, it will save project in your workspace folder… click next and finish.Its better .. infact recommend to configure your project, or workspace. Chose configure blackberry workspace and set options like vendor, version etcIn install component chose BlackBerry JDE Component Packs option.Great. You have set your workspace for blackberry application. Take a sip of coffee and take a bite of cookie. Now its time to learn few concepts … keep your coffee mug in your hand and concentrate on following paragraph. Keep taking sips of coffee.PUSH/POP of ScreensEvery software application has different pages to display different information and components for user interactions like buttons, text fields etc. In blackberry application, we will call them as screens. Mean we will design and develop different screens and will link them via different events like clicking a button will hide one page(screen) and will show other page (screen).This show/hide phenomenon in blackberry’s program is called pus and pop of a screen. Push mean displaying a screen and Pop means hide it. Its means when we want to display a screen, we first pop top screen and then push that screen we want to show. Hence application is behaving like a stack where we are push and pop screen and maintain a flow of screen as per events triggered by user.Got the idea? No!!! don’t worry. Read the above paragraph again and when clear move to next one. It’s quite simple han …Your First program for BlackBerryNow create a new package blackberry and create new Class. Name the new class “FirstBlackBerryClass”Every class that use user interaction controls must implement a class UiApplication (net.rim.device.api.ui.UiApplication). hey don’t smile .. I know the same class is available in iPhone sdk too, for same purpose 🙂Ok, you have created a class, have implement UiApplication. Now make a main() method to have entry point of the our application. This main method is same as traditional java classes … yes, you got it. Its our public static void main (String[] args)at this point our class should look likepackage com.iainteractive.blackberry;import net.rim.device.api.ui.UiApplication;public FirstBlackBerryClass extends UiApplication {public static void main(String[] args) {}public FirstBlackBerryClass(){}}Now in main method, creates an instance/object of your class and calls enterEventDispatcher(), inherited from UiApplication class. This method starts event handling and executes all drawing and event-handling code. Now your class should look likepackage com.iainteractive.blackberry;import net.rim.device.api.ui.UiApplication;public FirstBlackBerryClass extends UiApplication {public static void main(String[] args) {WelcomeBlackBerry firstApplication = new WelcomeBlackBerry();firstApplication.enterEventDispatcher();}public FirstBlackBerryClass(){}}Our class is read to execute, means at very very basic level you can say you have created your first application.Though it will do nothing. Now its time to add some screen. When our program will run, we should make sure that we have push a screen so that when application start execution, user can see something over his device. To do so, we will first create a screen and then will link it to our main class .i.e. FirstBlackBerryClass.Creating a ScreenBlackberry API provides a class name MainScreen that provides features common to standard RIM device applications like a title section, a separator element, and a main scrollable section (actually a single vertical field manager used to maintain a list of fields). So every class that behave like screen or in other words, every screen must extends MainScreen class to get access to different screen sections. So our next task is to develop a screen class. Add new class in package, name it as FirstScreenpackage com.iainteractive.blackberry;import net.rim.device.api.ui.container.MainScreen;final class FirstScreen extends MainScreen {FirstScreen(){}}Now we will add some element to display. First important thing is title for your application. For this, use LabelField class object and set some string as title. Then set this label as application’s title.LabelField appTitle = new LabelField(“HelloWorld Sample”, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);this.setTitle(appTitle);now to add some text over the screen, use RichTextField class and pass some string to display.RichTextField greeting = new RichTextField(“its running… great”);Now add it to screen by add() method of MainScreen class.this.add(greeting);now our FirstScreen class will look like thatfinal class FirstScreen extends MainScreen {FirstScreen(){LabelField appTitle = new LabelField(“HelloWorld Sample”, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);this.setTitle(appTitle);RichTextField greeting = new RichTextField(“its running… great “);this.add(greeting); }}Our screen is ready. Now time is to link it with our main program .i.e. FirstBlackBerryClass. We have learned that to show a screen in blackberry application, we have to push it, so to show our FirstScreen, we will push it to in our FirstBlackBerryClass class.For this we will use pushScreen method of UiApplication class that takes a reference of a Screen object. We will call this method in constructor of our FirstBlackBerryClass class so that as soon as our application run, it push our screen to display on user’s device.Now go back to FirstBlackBerryClass class’s constructor and the following line there.pushScreen(new FirstScreen());its done … we have done .. now our final code should look like that//======================package com.iainteractive.blackberry;import net.rim.device.api.ui.UiApplication; public class FirstBlackBerryClass extends UiApplication { public static void main(String[] args) {FirstBlackBerryClass firstApplication = new FirstBlackBerryClass();firstApplication.enterEventDispatcher();}public FirstBlackBerryClass(){pushScreen(new FirstScreen());}}//======================package com.iainteractive.blackberry;import net.rim.device.api.ui.container.MainScreen;import net.rim.device.api.ui.component.LabelField;import net.rim.device.api.ui.component.RichTextField;final class FirstScreen extends MainScreen {FirstScreen(){LabelField appTitle = new LabelField(“HelloWorld Sample”, LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);this.setTitle(appTitle);RichTextField greeting = new RichTextField(“its running… great“);this.add(greeting);}}//======================Test Your Application in the Simulatorfor testing application in device simulator, do the following steps, click on Run or the green shortcut icon on the toolbar. when your simulator starts, find and start your application from Downloads folder.When you run the application you should see its running… great message.To exit the simulator, just close its window.Aasim NaseemI’m an engineer by profession, a blogger and a photojournalist by hobby. Seasonal writer at LAFZ Media UK. I write on different topics and things around us. Contact me at +971.56.126.8842 or Aasim.Naseem@outlook.com BlackBerry Tips & Tutorials 3rd part of BlackBerry Application Development TutorialBlackBerry Application DevelopmentBlackBerry Application Development Tutorial (Part-2)BlackBerry Application Development Tutorial (Part-3)BlackBerry JDE Component PacksBlackBerry JDE plug-in for eclipseconfiguring eclipse with BlackBerryEclipse SDKFIRST APPLICATION FOR BLACKBERRYFirstBlackBerryClassJavaMainScreennet.rim.device.api.ui.container.MainScreennet.rim.device.api.ui.UiApplicationPUSH/POP of ScreensTest Your Application in the SimulatorUiApplicationWelcomeBlackBerry project
Hi, Nice article for beginners. By profession, I am also a software engineer. I have a query: I want to display LabelField at the bottom of screen in such a way that it automaticaly renders at the bottom of each screen of any size. Plz resolve this issue of mine. Waiting for reply….Thanks, AnishReply
Hello Ans, I have enjoyed this tutorial… I am now trying to expand this application to include three Numeric fields for user input, and buttons to calculate the total and the 3 fields, and to calculate the average of the 3 fields. I would appreciate any assistance you may wish to provide. Thanks and keep up the good work. Regards AlexReply
Really brother ur teaching style is awesome..i m really impress… Pls dont stop n keep posting such helping stuffs…Reply
Thanks for the tutorial.Am new to Java and haven’t tested the program yet, but isn’t there a mistake under the section “Your First Program for Blackberry”?package com.iainteractive.blackberry;import net.rim.device.api.ui.UiApplication;public FirstBlackBerryClass extends UiApplication {public static void main(String[] args) {WelcomeBlackBerry firstApplication = new WelcomeBlackBerry();firstApplication.enterEventDispatcher();}Shouldn’t the class instantiation be: FirstBlackBerryClass firstApplication = new FirstBlackBerryClass();It seems to be corrected in the full code for the program but it is misleading at the top.Reply
I like to share information that will I’ve built up with the 12 months to assist enhance group performance.Reply