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 knowincluding medon'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.