In this tutorial we are going to focus on one of tried and true ways to start developing applications in a new language or for a new platform; a hello world application. We will start the tutorial by learning how to make a Blackberry application project and finish with an application that will display “Hello World” when we click a button. The core, underlying language supporting the Blackberry framework is Java, this will mean it will be a little easier for people who know Java to code for Blackberry. With that being said, let’s start coding by starting a Blackberry project.


To do this we need to navigate to the toolbar in the top left of our Integrated Development Environment (IDE) and click the button that looks like a window with a yellow plus (+) symbol on the top right. After this you can proceed to choose the Blackberry folder and expand it. After it is expanded, you will want to choose Blackberry Project from the options and press the Next button. We will then be taken to to the screen requiring some information on the project we are creating.

We will want to name the project BlackberryHelloWorld. We are also given the option at this time to change the JRE or Java Runtime Environment as well as to create a new project in our workspace or import existing code to start us off. We will want to leave all as it is and continue onto the next screen by pressing Next.

When we reach the next screen we will see that we have the ability to alter the source code, project, and libraries on our new project. For this screen you can try exploring and learn what everything will do but we decided we wanted to go ahead and press next to progress towards the next screen.

On this new screen we now have the ability to choose what template we would like to use if any. We want to go with the Blackberry Application but there are also options for Hello Blackberry Template that creates a BlackBerry application that contains a screen populated with some simple UI components. We can also choose an Empty Application that creates an empty BlackBerry device application or we can choose an Empty Library that creates an empty BlackBerry device library for reuse in future projects. After you have Blackberry Application chosen we can proceed to press the next button.

This next screen we can choose the names for all of the initial files and variables we are about to use. Below is a bullet point list of what we have to fill in and what we used:

  • Package Name: com.binarywasteland
  • Application Class Name: HelloApp
  • Screen Class Name: HelloScreen
  • Screen Title: Hello World
After this is done we can press Finish and proceed to our newly created project where we will want to explore some of the auto-generated code. For example, below is the auto-generated code for HelloApp.java as we are only going to modify the HelloScreen.java for our Hello World application.

// HelloApp.java

package com.binarywasteland;

import net.rim.device.api.ui.UiApplication;

/**
* This class extends the UiApplication class, providing a
* graphical user interface.
*/
public class HelloApp extends UiApplication
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
HelloApp theApp = new HelloApp();
theApp.enterEventDispatcher();
}

/**
* Creates a new HelloApp object
*/
public HelloApp()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new HelloScreen());
}
}

In the code above we can see that HelloApp has created a class for itself and this class will push a new screen onto the Blackberry Simulator or device. In the main event we can see that HelloApp is initialized and calls the enterEventDispatcher which is used to render our new screen onto the device. We also need the import of import net.rim.device.api.ui.UiApplication; as it allows us the extend the UIApplication class used by Blackberry to present screens on a device onto our HelloApp class. Now that we have looked at the HelloApp.java we can now look at what it takes to make a screen and present it onto the Blackberry.

// HelloScreen.java

package com.binarywasteland;

import net.rim.device.api.command.Command;
import net.rim.device.api.command.CommandHandler;
import net.rim.device.api.command.ReadOnlyCommandMetadata;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

In the code above you will notice a lot more imports and that will be due to the code we are going to utilize below. We are going to use the Command and CommandHandler to attatch to the button and display an pop-up notifying the user of something. We import the MainScreen s that we can extend the HelloScreen class and utilize the functions and methods that the MainScreen class would be granted. The Field, ButtonField and Dialog are all objects that will be needed and called below.


/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class HelloScreen extends MainScreen
{
/**
* Creates a new HelloScreen object
*/
public HelloScreen()
{
// Set the displayed title of the screen
setTitle("Hello World");

// Create ButtonField with command context
ButtonField buttonField = new ButtonField("Click Me!", ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER);

// Set command to be invoked by the ButtonField
buttonField.setCommand(new Command(new CommHandDialog()));

add(buttonField);
}

In the code above we have the Title already generated for us as we gave it a name when creating the project. After that we are creating a ButtonField that will display Click Me! in the buttons display text and we are telling the button to be centred horizontally and we use ButtonField.CONSUME_CLICK not to call the navigationClick event but to prevent the side effect of having our Blackberry Menu appear as well as the dialog box. We then want to use the setCommand on the ButtonField to send the user over to the handle we have given above. After this is done we can then use the add(); method to add any object onto the screen.


// A CommandHandler implementation which will be executed unconditionally
class CommHandDialog extends CommandHandler
{
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
Dialog.alert("Hello World");
}
}
}

The code above is implementing a CommandHandler which is going to call a Dialog box to appear. To utilize the Dialog all we have to do is use the .alert(); method and add a string inside the method. After that when the button is clicked you will be able to see the CommandHandler fire and show the Dialog box with the string you chose.

As you can see above we have started our first steps towards developing applications for Blackberry. In the next tutorial we are going to develop a client application that will focus on the GUI. If you have any questions about the methods used above feel free to give us a comment below. Until the tutorial, Happy Hacking!

Share This