devxlogo

Developing Touch-Enabled Applications with the Blackberry Touch API

Developing Touch-Enabled Applications with the Blackberry Touch API

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.

BlackBerry JDE API

BlackBerry devices can run applications written in Java and in .NET. Within Java, developers can develop either pure Java ME applications or applications based on RIM’s proprietary SDK (featured in this article). Support for Java ME depends on the type of device and on the version of the SDK deployed on the device.

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).

Introducing Touch Programming for Blackberry

Three concepts constitute the foundation of Blackberry’s touch-oriented programming: taps, gestures, and the virtual keyboard. These concepts translate into programmable functions on Blackberry platforms. Touch-enabled devices running the Blackberry SDK recognize taps as light touches, double taps, press-holds, press-and-drags, and multi-touches (i.e., simultaneously touching two UI items). They recognize gestures as movements of one or more contacts (i.e., fingers) across the UI and in the four designated directions: NORTH, EAST, WEST, and SOUTH. The nature of the motion is represented as a swipe or a move along taps or clicks.

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.

  • Tapping and dragging gestures are recognized as two distinct groups of Touch events.
  • Taps and clicks are simple inputs compatible with more conventional command-button or trackball clicks.
  • Touch gestures are commands for reorganizing the screen, dragging items, and scrolling.

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.

Programming for Touch Events

The key to programming touch functionality is handling the supported touch events properly. This section details what these events are and how you can translate them into programs that enable custom touch functionality.

The following are the necessary steps for handling touch events:

  1. Extend one of the classes?Manager, Screen, Field, or some of the Field’s subclasses (depending on the scope of the event handling)?and override the method-protected Boolean touchEvent (TouchEvent event).
    private class NameField extends Field {   ...   protected boolean touchEvent( TouchEvent event ) {   ...   }// end method   ...}//end class
  2. Determine the type of touch by interrogating the instance of the net.rim.device.api.ui.TouchEvent class.
    // handle touch eventprotected 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.)

  3. When you invoke the touchEvent function, determine the event type by examining the type property and comparing it with the TouchEvent constants.
    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 method

The 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.

Gestures and Their Specializations

Gestures (instances of the class net.rim.device.api.ui.TouchGestures) logically are sub-types of TouchEvent. You determine gestures (e.g., swipes and taps) by checking if the trapped TouchEvent is a gesture:

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.

Double Clicks and Multi-Touch

Double clicks are special yet simple types of gestures. You determine a double-clicking event by counting the number of consequent clicks that occur on the touched surface:

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.

No WORA in Mobile Development

Most of Java developers that I know?including me?don’t like to break the central Java maxim of “write once, run anywhere” by developing proprietary applications that target only specific platforms. Mobile platforms are a different case, however. The two major Java-compliant mobile application platforms, Blackberry and Android, offer very rich proprietary support for the most popular and useful functions and they fill in the gaps with Java ME. On the other popular touch-enabled consumer phone, iPhone, Java ME is barely a consideration.

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist