BlackBerry is a line of mobile e-mail and
smartphone devices developed and designed by Research In Motion (RIM) since 1999
and the two BlackBerry development environments produced by RIM. The BlackBerry
Java Development Environment (JDE), and the BlackBerry JDE Plug-in for Eclipse,
you already learned in my previous article "Start developing BlackBerry
Application". Now we start working on BlackBerry User Interface.

When we talk about BlackBerry User Interface
the very first thing which you have to know that is Layout Managers.

What is Layout Manager

RIM's layout managers are akin to Swing layout
managers, and allow you to arrange lots of fields on the screen, it is
responsible for determining where a UI element will be positioned on the
display. The BlackBerry software library comes with a set of pre-made layout
managers, and the MainScreen class uses the VerticalFieldManager as its default
layout manager.

The Screen Class has "layout managers", but we
called it as Field Managers. We have several Screen Classes:

Hierarchy of Screen Class

java.lang.Object

--net.rim.device.api.ui.Field

--net.rim.device.api.ui.Manager

--net.rim.device.api.ui.Screen

This Screen is the base class for all screens.

The BlackBerry User interface API follows a
Fields/Layout Managers/Screens models: Fields are contained within layout
managers, which arrange and draw them in spacific positions. We have some built
in layout managers:

Built in Managers

We have four built in layout managers:

VerticalFieldManager : This layout manager simply lays out the fields you add to it, from top/bottom.

HorizontalFieldManager : This layout manager simply lays out the fields you add to it, from left to right, on one row.

FlowFieldManager : This layout manager lays out fields in a horizontal-then-vertical flow.

DialogFieldManager : This layout manager used for Dialogs and status screens. It handles an icon, a message, and an area to hold a list of user specified fields.

Now Lets Code:

Step 1 : Open BlackBerry Java
Plug-in/Eclipse.

Step 2 : Choose a Project from File/New.

Step 3 : Choose BlackBerry Project
wizard and click on Next.

Step 4 : Now give a name to your project
like I gave "Layouts" and click on Finish.

Step 5 : Go to the Project Explorer and
You will see two auto-generated class's "MyApp.java and MyScreen.java"
under Layouts project folder src/mypackage.

MyApp.java class is contains the
main(String[] args) method and so is where we define our application. If you
want any kind of user interaction, then one of your classes must extend this
class because it provides all the default behavior for such an app. Within the
main(String[] args) method, you must define an instance of the class like for
MyScreen.java.

Code Behind of MyApp.java Class

This class is auto-generated, you do not need to change any code in this
class let it be remain same:

Note : When you add a
lot of Field objects to a VFM, you should remember to make them FOCUSABLE
because the VFM allow the scrolling when the fields that are off the visible
portion of the screen are set to focusable otherwise it will not allow scrolling
to them.

Screenshot

Scroll horizontally and see the remaining
fields

Code for FlowFieldManager

When we work with FlowFieldManager, vertical scrolling is enabled by
default. As you add more fields, they will just be added beside the existing
ones.package
mypackage;
import
net.rim.device.api.ui.*;
import
net.rim.device.api.ui.component.*;
import
net.rim.device.api.ui.container.*;
public
final
class
MyScreen extends
MainScreen
{public
MyScreen()
{ // default
style is Field.USE_ALL_WIDTH | Manager.VERTICAL_SCROLL
FlowFieldManager ffm = new
FlowFieldManager();