BlackBerry Application Development Tutorial (Part-3)

Posted on Updated on

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.

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.

1

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.

2

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 etc

3

In 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 Screens

Every 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 BlackBerry

Now 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 like

package 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 like

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();

}

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 Screen

Blackberry 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 FirstScreen

package 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 that

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);

}

}

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 Simulator

for testing application in device simulator, do the following steps,  click on Run or the green shortcut icon on the toolbar. run

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.

Advertisements

12 thoughts on “BlackBerry Application Development Tutorial (Part-3)

    Anish said:
    December 14, 2009 at 08:51

    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,
    Anish

    amcquaid said:
    September 9, 2010 at 03:07

    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
    Alex

    […] Your First Application :: BlackBerry Application Development Tutorial (Part-3) […]

    […] Your First Application :: BlackBerry Application Development Tutorial (Part-3) […]

    […] Your First Application :: BlackBerry Application Development Tutorial (Part-3) […]

    Viraj said:
    July 5, 2011 at 12:58

    Really brother ur teaching style is awesome..i m really impress…
    Pls dont stop n keep posting such helping stuffs…

    Gamal said:
    July 28, 2011 at 09:47

    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.

    predhin said:
    July 31, 2011 at 12:19

    Can there be more than one class that extends UiAppClass for the same app

    Pimpkey said:
    September 22, 2011 at 20:45

    If u got an errer in the Blackberry_App_Descriptor.xml install the JDK6…

      Ans responded:
      September 23, 2011 at 18:15

      Thanks for your update on this point;

    SAndip Dube said:
    May 21, 2012 at 15:29

    Awasome tutorial…very nice..Thanks

    Anonymous said:
    July 22, 2013 at 09:46

    I like to share information that will I’ve built up with the 12 months to assist enhance group performance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s