Adding a Header Background

We want to put a black background behind the header image and align the image to the left. The second change is easy simply change the style Field.FIELD_HCENTER to Field.FIELD_LEFT or leave it out entirely, as FIELD_LEFT is the default . To make the image sit on a different color background, we'll put the BitmapField inside another manager and let that manager draw the background color. We'll use a HorizontalFieldManager. This first part should be familiar to you by now. Change the following...

Whats Different About Developing for BlackBerry

If you're familiar with developing for modern desktop PCs or servers, there are many things you might not think about before starting to develop for BlackBerry. Of course, every platform is different, but there are some things to keep in mind when designing and implementing an application for BlackBerry. Generally, BlackBerry CPU speed and RAM - as with most mobile devices - lag a few years behind average PCs. The latest BlackBerry devices are getting faster, so this isn't as much of a...

Making Secure HTTP HTTPS Connections

To make a connection to a secure HTTP server, replace http with https in the Connector.open method. Fortunately, the web application also supports HTTPS connections, so simply substitute https into the URL to make and then click Get. The result will look almost the same as the non-secure HTTP connection see Figure 7-14 . Figure 7-14. Retrieving the web application over HTTPS Figure 7-14. Retrieving the web application over HTTPS Performing the POST works in a similar way. We haven't had to...

Creating the Domain Field and Check Box

The Domain field should be a drop-down list. For the BlackBerry, this is accomplished by an instance of You can implement the interface directly, but for this application, the net.rim.device.api.ui.component. ObjectChoiceField component will do just fine it allows us to specify an array of Objects, which will be used to populate the field the toString method will be used for the display string . If you want a list of numbers, net.rim.device.api.ui.component. NumericChoiceField is also often...

Delegate Managers

A screen doesn't directly lay out any of its fields. Instead, it delegates that to a manager that's specified when the screen is instantiated. All the manager methods on the screen add, delete, insert, etc. actually end up invoking the same methods on the delegate manager. The only component the screen handles directly is the delegate manager. This separation of manager and screen makes it easy to change the internal layout of any screen. This also means that a screen must have a delegate...

BlackBerry Development

Learn how to build Java-based BlackBerry applications from scratch All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 electronic 978-1-4302-2428-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in...

The ALX File

Desktop installation requires a different type of descriptor file than OTA installation. For desktop installation, the file is XML based and has the extension .alx. Here's an example ALX file for UiFun lt loader version 1.0 gt lt application id UiFun gt lt name gt The Beginning BlackBerry UI Fun Application lt description gt lt version gt 1.0.1 lt version gt lt vendor gt Anthony Rizk lt vendor gt lt copyright gt Copyright c 2009 Anthony Rizk lt copyright gt lt fileset Java 1.39 gt lt directory...

Implementing the Login and Clear Menu Items

We'll implement two menu items for our application corresponding to the Login and Clear actions. We'll create a new class for each menu item and declare these as inner classes within UiFunMainScreen because we'll only use them here and to give them access to UiFunMainScreen's private login and clearTextFields methods public class UiFunMainScreen extends MainScreen implements FieldChangeListener class LoginMenuItem extends MenuItem public LoginMenuItem super Login, 20, 10 class ClearMenuItem...

Animating the Layout

protected void sublayout int width, int height super.sublayout width, height if verticalOffset gt 0 if animationStart 0 start the animation animationStart System.currentTimeMillis long timeElapsed System.currentTimeMillis - animationStart if timeElapsed gt animationTime verticalOffset 0 float percentDone float timeElapsed float animationTime verticalOffset Display.getHeight - int percentDone Display.getHeight Runnable public void run updateLayout Notice that there's an initial case where...

Connection Method Using Direct TCPIP Instead of BESMDS

Now, let's take a few minutes to see how to force a connection to make a direct TCP IP connection to the server instead of using the BES MDS. The Networking application currently uses the device's default connection method. As mentioned earlier, if you run the application on a device that's not activated on a BES, the requests will already go over direct TCP IP. However, if you run on a device connected to a BES, the POST and GET requests are done through the BES. To force them to go directly,...

Creating a Custom Screen

We're actually using three screens in our UI Fun application already. Two are obvious UiFunMainScreen and the login success screen. The third is the dialog that appears when you try to log in without entering a username and password see Figure 5-24 . Figure 5-24. The login dialog is a Screen too Figure 5-24. The login dialog is a Screen too A screen on the BlackBerry doesn't have to take up the entire display other screens can be visible below it. All screens, however, do take over the user...

Tidying Up the Login Success Screen

Now that we've got a few components, let's revisit LoginSuccessScreen. With some simple modifications to the constructor to use our CustomLabelField and GridFieldManager, we can make the screen look a little better. public LoginSuccessScreen String username, String domain try FontFamily alphaSansFamily FontFamily.forName BBAlpha Serif Font appFont alphaSansFamily.getFont Font.PLAIN, 9, Ui.UNITS_pt setFont appFont catch ClassNotFoundException e add new CustomLabelField Logged In , Color.WHITE,...

Making the Button Focusable

To make the button focusable we'll just override isFocusable in CustomButtonField to return true public boolean isFocusable return true Now, you'll be able to move the focus down to the buttons, but the visual representation shown in Figure 5-10 is not what we want. Figure 5-10. Custom button fields with the default focus drawing behavior Figure 5-10. Custom button fields with the default focus drawing behavior

Location Documents

BlackBerry Maps defines an XML document format that you can use to specify view information, location markers, and route information while invoking BlackBerry maps. The basic format of a document showing one or more locations is lt location y 'latitude' x 'longitude' label 'Location_Label' description 'Description' gt lt location y 'latitude' x 'longitude' label 'Location_Label' description 'Description' gt lt location y 'latitude' x 'longitude' label 'Location_Label' description 'Description'...

Handling the Clear Button

We'll hook up the Clear button first. Add the following line in the constructor, just after instantiating the button Now, when the user clicks on the clear button, we'll receive an event in UiFunMainScreen.fieldChanged. We can test this with a simple dialog using the net.rim.device.api.ui.component.Dialog class public void fieldChanged Field field, int context if field clearButton Dialog.inform Clear Button Pressed The Dialog class is a handy way of displaying simple messages to the user. Run...

The HttpRequestDispatcher Class

We need to create a separate thread for the HTTP request. Do this by creating a new class that extends java.lang.Thread. Call the class HttpRequestDispatcher public class HttpRequestDispatcher extends Thread private String url private String method GET or POST private NetworkingMainScreen screen public HttpRequestDispatcher String url, String method, NetworkingMainScreen screen this.url url this.method method this.screen screen Notice we're passing in an instance of the main screen. This is to...

Different Ways to Connect

The BlackBerry offers a number of different methods of making network connections. These include the BlackBerry Enterprise Server BlackBerry Mobile Data System BES MDS , direct Transmission Control Procotol Internet Protocol TCP IP , BlackBerry Internet Service BIS , WiFi, Wireless Access Protocol WAP 1.0, and WAP 2.0. See Figure 7-1. They each have certain advantages and disadvantages, and depending on the configuration of your device and environment, some of the methods might not be...

Displaying a Custom Map View

In addition to letting BlackBerry Maps automatically position and zoom the view, you can specify a view in terms of a latitude, longitude, and zoom level. To create a custom map view, you create an instance of net.rim.blackberry.api.maps.MapView with a latitude, longitude, and zoom. The zoom level ranges from 0 zoomed all the way in to MapView.MAX_ZOOM zoomed all the way out . The view will be centered on the given latitude and longitude. Let's add this functionality to our Location program....

To Use Assisted and Unassisted Modes

With this criteria, the initial location is retrieved using Assisted GPS subsequent locations are fully unassisted Accuracy is 50 metres, cost is allowed, and preferred power consumption is high Criteria criteria new Criteria criteria.setHorizontalAccuracy 50 criteria.setVerticalAccuracy 50 criteria.setCostAllowed true

Taking a Look

Replace the ButtonFields in UiFunMainScreen with CustomButtonFields, and you'll see how the buttons look so far. In UiFunMainScreen's constructor, change the class for the button declaractions from ButtonField to CustomButtonField CustomButtonField clearButton CustomButtonField loginButton Then, in UiFunMainScreen's constructor replace the following lines clearButton new loginButton new clearButton new CustomButtonField Clear, Color.WHITE, Color.LIGHTGREY, 0 loginButton new CustomButtonField...

Adding the Image to the Screen

BitmapField takes a Bitmap object as an argument in its constructor, so we'll need to load the image before constructing the field. We'll import net.rim.device.api.system.Bitmap and add the following line to UiFunMainScreen's constructor load the bitmap from the image resource we just added Bitmap logoBitmap Bitmap logoBitmap The path for loading bitmaps, and all other resources, is relative to the location of your project definition file the .jdp file . We put the bitmap in a res folder at the...

Creating an Alternate Entry Point

An alternate entry point is exactly what it sounds like another way for the user or BlackBerry to start your application. An alternate entry point can provide another icon on the BlackBerry home screen to start your application. By clicking on the other icon, the same main method is called, but with different parameters, allowing you to run your application in different modes. Alternate entry points are also commonly used with applications that are configured to automatically start when the...

Creating Basic Animation

We're going to spend a little bit of time on a topic that can really make your user interface shine animation. Now, this topic is more advanced, and really only recently have applications with fancier screen transitions and other effects started to appear. So you're fine without animation. This section will teach you a bit more about how the BlackBerry UI API works though, and it's kind of fun, so I recommend you at least read through it. The BlackBerry platform, at the time of this writing,...

Seeing the Grid Field Manager in Action

OK, now let's see how the grid looks. We'll modify UiFunMainScreen's constructor again to use the grid field manager to hold our separate labels and edit fields usernameField new EditField , LabelField usernameLabel new passwordField new PasswordEditField , LabelField passwordLabel new domainField new ObjectChoiceField , new String Home, Work LabelField domainLabel new LabelField Domain , Field.FIELD_RIGHT GridFieldManager gridFieldManager new GridFieldManager 2, 0 Remember, because we've...

Copying the File

To display the file name screen, we'll use UiApplication.pushModalScreen instead of pushScreen. This just means that the method won't return until the file name screen is closed, this is the functionality we want in this case because we can't copy the file until you've entered a file name and clicked OK FileNameScreen screen new FileNameScreen String newFilename screen.getFilename After getting the file name, we'll open a connection to the full URL for that filename and use...

The Location Application

Now let's put all this knowledge to use and create an application that uses the location API. As you should expect by now, you'll start by creating a new BlackBerry CLDC application. Call it Location. Create a main application class and a main screen class called LocationApp and LocationMainScreen, respectively both are in the com.beginningblackberry.location package. You'll add a few fields to LocationMainScreen, a menu item to update the location, and a skeleton update method. The initial...

Using the Event Lock

What about the second method of updating the UI from another thread With a simple modification to MainScreenUpdaterThread we can do that too. Change the run method of MainScreenUpdaterThread to the following for int i 0 i lt 10 i try catch InterruptedException ex Ensure we have the event lock The application should run exactly the same way as in the previous example. This example application is admittedly a bit contrived, but it's important to understand the concept of updating the UI from a...

Modifying NetworkingMainScreen

We need two things in our screen an edit field to enter the post data and a way to invoke the Post request a menu item and method . Add the edit field first. Declare a new EditField called postDataField Then, initialize it and position it right below the URL field postDataField new EditField Post data , add new LabelField Image retrieved add imageOutputField Next, define the postURL method. It does the same thing as the getURL method with the additional functionality of taking the text from the...

MapField Embedding BlackBerry Maps in Your UI

From OS 4.5 and above, the BlackBerry platform provides the capability to embed a BlackBerry Maps view into your application's user interface. You will add a map field to the user interface and have it update whenever a new location update happens. Because this works only with JDE v4.5 and higher, be sure that you're running an appropriate version of the JDE or that you've set your Eclipse workspace to use JDE v4.5 or higher. The MapField lets you set a map position, but it doesn't support...

Implementing the Screens sublayout Method

As we've discussed, the screen's sublayout method needs to worry about only the delegate manager. This is accessible through the getDelegate method. There are a couple of special methods in Screen that allow us to layout the delegate manager and position it. These methods work the same as the methods to set field position and size in any other manager. In addition to this, our sublayout method needs to set the extent of the screen, just as with any field, and set the position of the screen on...

Implementing the sublayout Method

All the magic in a manager happens in the sublayout method. It's similar to a field's layout method, down to the requirement that you call setExtent within the method to set the manager's size and in fact, behind the scenes, the manager's layout method calls sublayout . Remember, a manager is ultimately a type of field. The other requirement in a manager's sublayout method is that you position and size all the fields contained by the manager. This is done through the layoutChild and...

Creating an Application with the BlackBerry JDE Plugin for Eclipse

The JDE Plug-in for Eclipse hasn't been around as long as the stand-alone JDE and, until fairly recently, wasn't as well supported, so there are still a few things that may not work 100 percent of the time. Also, the JDE Plug-in supports only JDE version 4.2.1 and later. Neither of these limitations is a concern for the majority of projects the flakier parts are fairly obscure and don't affect the final application, and version 4.2.1 is pretty much the minimum that most commercial applications...

R

rapid application development RAD , 1 Record Management System RMS , 132 relative positioning, 125 requestBackground method, 55 requestFailed method, 172, 175 requests, HTTP, 166, 176-177 Research in Motion RIM , 1, 8 Resources section, 237 response codes, HTTP, 166, 176 RichTextField class, 168-169, 176 RIM Research in Motion , 1, 8 RIM_APP_WORLD_EMAIL value, 231 RIM_APP_WORLD_ID value, 231 RIM_APP_WORLD_LICENSE_KEY value, 231 RIM_APP_WORLD_NAME value, 231 RIM_APP_WORLD_PIN value, 231...

The Persistable Application

Create a new BlackBerry application project called Persistable. The main screen will have a few controls on it, so we can demonstrate persistence of different data types. To save time, here's the code for both classes package import public class PersistenceApplication extends UiApplication public PersistenceApplication PersistenceScreen screen new PersistenceScreen pushScreen screen public static void main String args PersistenceApplication application new PersistenceApplication...

Getting App World Properties for Your Application

App World embeds a number of properties in applications that are downloaded and purchased through App World these properties include the license key if a license model other than static was used , the App World application name as specified in the Vendor Portal, the e-mail of the application purchaser, and others. Through the BlackBerry API, your application has access to these properties, so, for example, you can easily display license information to the user or pass license key information to...

Basic Application Framework

You should be used to creating applications by now create a new a new BlackBerry application called FileConnection. Start with an application class and main screen class. The initial classes are as follows package import public class FileConnectionApplication extends UiApplication public FileConnectionApplication FileConnectionScreen screen new FileConnectionScreen pushScreen screen public static void main String args FileConnectionApplication app new FileConnectionApplication...

Launching App World from Your Application

With OS 4.3 and later, you can use the javax.microedition.content package to launch App World from your BlackBerry application and display a specific application when it launches. This is useful if you want to provide links to other applications you have developed or provide a way from your app to easily open App World for a user to purchase your application after a trial has expired or download an updated version. The following code will launch App World and open the application whose ID is...

Creating an Application with the BlackBerry JDE

The BlackBerry JDE is a fully integrated development environment that contains all the tools you need to develop, build, and test your application, on both a simulator and a device. Its editor, while quite usable, is not quite state-of-the-art, and if you re used to something like Visual Studio or Eclipse, you may find it a step backward. However, it makes up for this by supporting the widest range of features for BlackBerry development there are times when only the JDE will do. And a lot of...