ollowing the introduction of its BlackBerry Storm touch phone, Research in Motion (RIM) released the BlackBerry Java Development Environment (JDE) API 4.7.0 (with version 5 in beta). This latest version of the JDE supports development of custom applications for touch-based user interfaces.
RIM is keeping up with the latest Java ME standards as well, so their latest development environments generally support all of the latest Java ME specs (e.g., MIDP 2.0/CLDC1.1). BlackBerry devices also run Java applications written using the proprietary BlackBerry SDK, which provides functions beyond those offered through Java ME. These functions are specifically optimized for BlackBerry devices and specific models (e.g., touch functions for the Storm).
This article surveys all the major touch programming features supported through the BlackBerry JDE API and provides Java code snippets to demonstrate how to program for these features.
| Author's Note: For more information about the mapping between devices, the SDK, and Java ME compatibility, consult the Blackberry's very elaborate, albeit hard-to-navigate, web site for software developers, which hosts a multitude of very useful materials. To make the most of this resource, first spend some time reviewing the Blackberry software development videos and API documentation. After that, study the Javadoc for the Blackberry SDK. It provides the most detailed information for Blackberry programming. |
For development, RIM favors Eclipse 3.4.0 or 3.4.1 as the default development platform. The company supplies a BlackBerry IDE (fully featured with a debugger and high-fidelity, multi-device simulator) as a free Eclipse plug-in (featured in this article).
The virtual keyboard is a built-in UI component (an instance of the net.rim.device.api.ui.VirtualKeyboard class) that the Blackberry device automatically displays when the user enters the input field UI component. Alternatively, you can programmatically display the virtual keyboard by retrieving the virtual keyboard singleton from the screen and executing the show method on the instance.
VirtualKeyboard keyboard;
MainScreen screen = new MainScreen( MainScreen.VERTICAL_SCROLL );
keyboard = screen.getVirtualKeyboard();
keyboard.setVisibility( VirtualKeyboard.SHOW );
The following groups of commands are modeled as constants in the net.rim.device.api.ui package of the TouchEvent and TouchGesture classes.
Devices that run off the Blackberry SDK 4.7+ and are touch enabled by default can support touch functionality even for applications that were not initially developed for touch. Using the auto-activated virtual keyboard, the SDK automatically handles events such as clicking, scrolling, and entering text into edit fields through taps, gestures, or entries.
These functions are limited to only a few very basic behaviors, however. Developers must develop their own handlers in custom code for more advanced and specialized user interface (UI) interactions.
| Author's Note: The layout of the virtual keyboard depends on the orientation of the screen. The landscape mode shows the virtual keyboard in full QWERTY display. The portrait mode shows the virtual keyboard in an abbreviated display, where two characters are placed in one field so the actual value of the user's key selection is retrieved by counting the number of times that key is clicked. |
In addition to allowing you to control the appearance and layout of the keyboard, the Blackberry SDK also supports programmatic control of the spellchecking events. This feature is beyond the scope of this article, but if you are interested in programming for the virtual keyboard spellchecker, check the classes and supported events within the net.rim.blackberry.api.spellcheck package.
The following are the necessary steps for handling touch events:
private class NameField extends Field {
...
protected boolean touchEvent( TouchEvent event ) {
...
}// end method
...
}//end class// handle touch event
protected boolean touchEvent( TouchEvent event ) {
int eventCode = event.getEvent();
if ( eventCode == TouchEvent.CLICK ) {
//Handle click event here
}
...
If ( eventCode == TouchEvent.MOVE ) {
//Handle move event here
}
...
In addition to the demonstrated TouchEvent, other common touch event types include TouchEvent.DOWN for light touch, TouchEvent.TAP for tapping, TouchEvent.SWIPE for the swiping gesture, TouchEvent.CLICK for pressing, and TouchEvent.HOVER for press and hold. (Refer to the Blackberry API documentation for a list of all supported event types.)
protected boolean touchEvent( TouchEvent event ) {
switch( event.getEvent() ) {
case TouchEvent.CLICK:
//handle click event here
return true;
case TouchEvent.DOWN:
//handle down event here
return true;
case TouchEvent.MOVE:
///handle move event here
return true;
}
return false;
}//end methodThe example above demonstrates the essence of all the touch-event processing within the Blackberry SDK. Any differences are related to the specifics of sub-event recognition, such as for Gestures, and to the additional properties related to these subclasses.
If ( eventCode == TouchEvent.GESTURE ) {
//handle gesture here, see the next example
If the TouchEvent is a GESTURE, then actual sub-type of the gesture is determined by examining the TouchGesture type:
TouchGesture gesture = event.getGesture();
int gestureEventCode = gesture.getEvent();
if (gestureEventCode == TouchGesture.TAP) {
//handle tap event here
}//end if
...
A swipe is a gesture characteristic for touch-enabled devices that enables animations and sliding of UI components. To properly interpret the properties of a swiping gesture event, you also must determine the direction of the gesture:
protected boolean touchEvent( TouchEvent event ){
switch( event.getEvent() ) {
case TouchEvent.GESTURE:
TouchGesture gesture = event.getGesture();
switch( gesture.getEvent() ) {
case TouchGesture.SWIPE:
if( gesture.getSwipeDirection() == TouchGesture.SWIPE_SOUTH ){
//Handle swipe south event
return true;
}//end if
return true;
}//end case
}//end case
return true;
}//end touch event handler
You then use the direction to execute custom functionality such as closing, removing, or sliding UI components.
protected boolean touchEvent( TouchEvent event ) {
switch( event.getEvent() ) {
case TouchEvent.GESTURE:
TouchGesture gesture = event.getGesture();
switch( gesture.getEvent() ) {
case TouchGesture.TAP:
if( gesture.getTapCount() == 2 ) {
//it is a double click, handle it here
return true;
}//end if
}//end switch
}//end switch
return false;
}//end method
Notice the use of the method getTapCount() to identify the double click. As an exercise, explore what happens if you trap more than two clicks.
Think of multi-touch as a combination of special-character keystrokes and mouse clicks on your favorite operating system (e.g., selecting multiple files by pressing the control key and then clicking on the file names). In the case of a touch device, the user would most likely press multiple UI items with different fingers simultaneously.
You can recognize multi-touch events on the Blackberry Touch API by examining the touch event for existences of multiple coordinates for multiple touches:
protected boolean touchEvent( TouchEvent event ) {
switch( event.getEvent() ) {
case TouchEvent.MOVE:
if( event.getX(1) > 0 && event.getY(1) > 0) {
//handle here location of the first finger
}//end if
if( event.getX(2) > 0 && event.getY(2) > 0 ) {
//handle here location of the second finger
}//end if
return true;
}//end switch
If the user pressed the screen in multiple places, TouchEvent will record a separate pair of coordinates (with values greater than zero) for each touch point.
| Author's Note: Note that the API indexes coordinates with index values starting at 1. Most indexes in Java data structures (such as arrays, subscripts, and collections) start with 0. Keeping this unfortunate starting-at-1 indexing scheme in mind may save you some debugging time. |
Therefore, Java developers who are interested in developing attractive and functional applications for mobile devices will most likely need to explore at least some proprietary functions like those presented in this article.
One can only hope that mobile platform developers standardize on some new, feature-rich release of Java ME in the future.
| DevX is a division of Internet.com. © Copyright 2010 Internet.com. All Rights Reserved. Legal Notices |