Random thoughts shooting out of volatile mind
Blackberry Sample Application
Blackberry uses custom J2ME so for developping application for BlackBerry you need to have JDE (Java DEvelopment Environment) from RIM (Research In Motion).
Its a common practise in any computer language  we learn is to start with Hello World ! example. As usual i'm going to give a brief intro on BlackBerry application using a Hello World but i've added a bit more that is how to do screen navigation in blackberry application. Well below is the sample code which I prepared.
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
public class HelloWorldDemo extends UiApplication {
private MainScreen _screen;
private ButtonField _nextScreen;

public static void main(String[] args) {
HelloWorldDemo instance = new HelloWorldDemo();
instance.enterEventDispatcher();
}
public HelloWorldDemo() {
_screen = new MainScreen();
_nextScreen = new ButtonField("Go to Next Screen",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_nextScreen.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field,int context) {
pushScreen(new NextScreen());
}
});
_screen.setTitle(new LabelField("Hello World Demo",LabelField.USE_ALL_WIDTH));
_screen.add(new RichTextField("Hello to the BlackBerry World!",Field.NON_FOCUSABLE));
_screen.add(_nextScreen);
pushScreen(_screen);
}
}
class NextScreen extends MainScreen {
public NextScreen() {
setTitle(new LabelField("Second Screen !",LabelField.USE_ALL_WIDTH));
add(new RichTextField("This is new screen",Field.NON_FOCUSABLE));
ButtonField _btnGoBack = new ButtonField("Go Back",ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
_btnGoBack.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field,int context) {
UiApplication.getUiApplication().popScreen();
}
});
add(_btnGoBack);
}
}
Well blackberry uses different program structure than MIDLETs and is popularly called as RIMLETs or CLDC applications. Let me describe the structure of a CLDC application.
As you can see program has a public class which contains a main() method which is not there in MIDP applications. Normally most of the screen related operation is done in constructors and class containing main() method extends net.rim.device.api.ui.UiApplication class. In my example class HelloWorldDemo extends UiApplication. In the constructor for this class we create instance of NextScreen and push it on to display stack using pushScreen(Screen screen) method.
For designing a screen your class must either extend the Screen class or its subclasses or it should have instance of subclasses of Screen. Here the class NextScreen extends the MainScreen. To add a element to screen we use add(Field field) method. For adding menu items we can use addMenuItem(MenuItem menu) method. I've used add method to add a new RichTextField(non-editable) and a ButtonField.
Now you must be thinking how to know when button is clicked. For this you need to register a method using setFieldChangeListener(FieldChangeListener listener). Here FieldChangeListener is an interface with a method fieldChanged which I implemented by creating instance for interface there itself. Whatever is need to be done when button is clicked is written inside this method. As you can see I'm using popScreen method when button Go Back is clicked what this method does is poping the current screen from display stack i.e you will be seeing first screen when you click on go back button.
Below is the screen shots of the first application you developped.

Screen - 1

Screen - 2


Ok I have used JDE v4.5 since my system memory doesn't allow me to run the latest version JDEv4.7. And one good thing about BlackBerry is applications are always forward compatible so if you developed the application using v4.5 it will run without any problem in higher version but vice versa doesn't hold good :)
Posted by: copyninja on Sunday, 2 August 2009

blog comments powered by Disqus
Fork me on GitHub