BlackBerry 6.0 OS UI components - ToolBar

BlackBerry 6.0 OS UI components - ToolBar
Page content

Introduction

One of the components that I consider interesting to talk about from those included in BlackBerry 6.0 O.S. is the Toolbar component.

Toolbars provide users with a quick and easy way to access frequent actions for an application or screen. Each toolbar consists of a set of icons that appears along the bottom of the screen.

Please take into account that only Storm series smartphones and the BlackBerry 9800 Torch are able to use toolbars in their applications.

The code

Before you get started it’s a good idea to check you have the ability to use toolbar with the following piece of code:

ToolbarManager.isToolbarSupported()

This way we can easily decide if it will work before adding the component.

Let’s have a look to the example code:

First we create the ToolbarManager and we add it to the MainScreen using setToolbar method (this method will do nothing if isToolbarSupported returned false).

ToolbarManager manager = new ToolbarManager();

setToolbar(manager);

Now we have to create one or several ToolbarButtons and add them to the ToolbarManager we previously created. Each toolbar field has an ordinal specifying the sort order within the toolbar. The items are sorted left-to-right on the toolbar (with the higher ordinal on the left). In this example we create a button with an image and an String, created through a StringProvider, i.e, a String that can be changed.

try {

Bitmap myBitmap = Bitmap.getBitmapResource(“myImg.jpg”);

Image myImage = ImageFactory.createImage(myBitmap);

ToolbarButtonField button1 = new ToolbarButtonField(myImage, new StringProvider(“butn1”));

Now we have to develop the action we want the button to implement when it’s clicked. For that we use the setCommand method. This sets the Command to invoke when a ToolbarButtonField is activated. It also updates the enabled state of the ToolbarButtonField, depending on whether the command can execute or not. In this case, the action is defined in Command creation, through CommandHandler’s execute method. This is just a new Dialog alert window with the following text:” Click on button 1!!”.

button1.setCommandContext(new Object() { public String toString() { return “Button1”; } });

button1.setCommand(new Command(new CommandHandler() {

public void execute(ReadOnlyCommandMetadata metadata, Object context) {

Dialog.alert(“Click on button 1!!”);

} }));

We can create more buttons using a previous piece of code. At the end, we have to just add the buttons to the toolbar. You can play using false ToolbarSpacers to obtain a custom arrangement of your buttons. A toolbarspacer is a spacer used in ToolBarManager or ToolBarSetManager when a particular arrangement of ToolBarButtonFields is desired. It has no image, no text, no tooltip and is disabled by default. It occupies the same amount of space as any other

ToolBarButtonField. manager.add(new ToolbarSpacer(0)); manager.add(button1);

}catch (Exception e){

System.out.println(e.getMessage());

}

Toolbars add also a good way to interact with application functionalities without having to click on the BlackBerry button to show them and they make the application options more intuitive for the user.

This post is part of the series: BlackBerry UI Development

Working with the user interface of BlackBerry 6 OS. Learn about different UI components and how to use them with some easy to follow code samples and snippets.

  1. BlackBerry 6.0 OS UI components - RichList
  2. BlackBerry 6.0 OS UI components - ToolBar
  3. BlackBerry 6.0 OS UI Components - Activity Indicator
  4. BlackBerry 6.0 OS UI Components - Progress Indicator
  5. BlackBerry 6.0 OS UI Components - Table