Responding to touch screen events

BlackBerry® device users can perform the same actions on a device with a touch screen as they can on a BlackBerry device with a trackball. For example, BlackBerry device users can use the touch screen to open a menu, scroll through a list of options, and select an option.

If you use a version of the BlackBerry® Java® Development Environment earlier than version 4.7 to create a BlackBerry device application, you can alter the .jad file of the application to enable the application to respond when a BlackBerry device user touches the touch screen. In the .jad file, you would set the RIM-TouchCompatibilityMode option to false.

If you use BlackBerry JDE version 4.7 or later to create a BlackBerry device application, you can enable the application to identify the action the BlackBerry device user performs on the touch screen by extending the net.rim.device.api.ui.Screen class, the net.rim.device.api.ui.Field class, or one of the subclasses of the Field class and overriding the touchEvent() method. Within the touchEvent() method, compare the value that TouchEvent.getEvent() returns to the constants from the net.rim.device.api.ui.TouchEvent class and the net.rim.device.api.ui.TouchGesture class.

The TouchEvent class contains the constants that represent the different actions that a user can perform on the touch screen. For example, the click, touch, and move actions correspond to the TouchEvent.CLICK, TouchEvent.DOWN, and TouchEvent.MOVE constants.

The TouchGesture class contains the constants that represent the different gestures that a user can perform on a touch screen. For example, the tap and swipe gestures correspond to the TouchGesture.TAP and TouchGesture.SWIPE constants.

Code sample: Identifying the action a user performs on the touch screen

The following code sample uses a switch statement to identify the action.

protected boolean touchEvent(TouchEvent message) {
switch(message.getEvent()) {
    case TouchEvent.CLICK:
    Dialog.alert("A click action occurred");
    return true;

case TouchEvent.DOWN:
    Dialog.alert("A down action occurred");
    return true;

case TouchEvent.MOVE:
    Dialog.alert("A move action occurred");
    return true;
}

return false;
}

Was this information helpful? Send us your comments.