Developping Blackberry Apps: Hello World

I almost forgot about this post. It got stuck in the draft queue for a few weeks now, but since I’m running low on postable content, I decided to resurrect it, clean it up and post it.

As promised, I wanted to show you how to create a simple Blackberry application in Java. Of course since this is Java, we will be typing way more than it is logically necessary. For example, a simple Hello World application requires no less than two classes, with a full complement of boring boilerplate code. If you are a long time Java programmer you probably just went “Only two? Phew! I thought it was worse”.

Our first class is basically there just to hold the main function, because that’s what you do in Java. You create a class, then you declare your main function, and then you writhe your procedural code inside:

import net.rim.device.api.ui.UiApplication;
 
public class HelloWorld extends UiApplication
{
	public HelloWorld()
	{
		pushScreen(new HelloScreen());
	}
 
	public static void main(String[] args)
	{
		HelloWorld instance = new HelloWorld();
		instance.enterEventDispatcher();
	}
}

There are two things here that might require explanation. The pushScreen method takes an UI container as a parameter and then paints it on screen. In our case this container is going to be a class extending net.rim.device.api.ui.container.MainScreen – I will show you how to create one in just a second.

The enterEventDispatcher method enters the main loop and handles capturing and dispatching events. The rest should be self explanatory.

Now for the actual GUI container:

import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
 
public class HelloScreen extends MainScreen
{
	public HelloScreen()
	{
		super();
		LabelField applicationTitle = 
			new LabelField("My First Applicaiton");
		setTitle(applicationTitle);
 
		LabelField hello = 
			new LabelField("Hello World!");
		add(hello);
	}
}

As you can see, blackberry apps are built just like any other Java GUI apps. You initialize container, label, field and button objects, you add them to each other creating hierarchies. There are some layout classes that can help you out and if you need to capture events you attach a listener to an object just like you would do it in Swing app. This is how our app looks like in the emulator:

A simple hello world app is not very pretty too look at. Click to embigen.

A simple hello world app is not very pretty too look at. Click to embigen.

It’s simple and kinda ugly, but that’s really all we were going for. It is remarkably easy to create simple, create apps this way. You will however quickly notice that slapping few fields and labels together may work OK for other blackberry models, but is not the best way to make an app for the storm. Because of the way the interface works, it can be sometimes difficult to switch between fields that are very close to each other. Designing usable and good looking Storm apps takes a little practice. But more on that later..

This entry was posted in programming and tagged , , , . Bookmark the permalink.



5 Responses to Developping Blackberry Apps: Hello World

  1. Mart SINGAPORE Mozilla Firefox Windows Terminalist says:

    First baby steps to a full fledged CRPG for Blackberry!

    Reply  |  Quote
  2. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Mart:

    I know right. I should like port Nethack for blackberry.

    BTW, did you know there is NO FUCKIN NETHACK port for the device? It’s a crime!

    Reply  |  Quote
  3. Shrinivas Kudva INDIA Mozilla Firefox Windows says:

    Hi Luke!

    I had not realized it was so simple to write an app for mobile phones. Thanks! :)

    BTW, Is it necessary to include “super()” in the HelloScreen constructor?
    I thought the compiler automatically inserted a call to the constructor of the parent class.

    Cya :)

    Reply  |  Quote
  4. Pingback: Developping Blackberry Apps: Dice Roller « Terminally Incoherent WordPress

  5. J UNITED STATES Internet Explorer Windows says:

    @ Luke Maciak:
    Dude, totally. Project? Hit me up.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>