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.

devx-admin

devx-admin

Share the Post:
Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This

Razer Discount

Unbelievable Razer Blade 17 Discount

On September 24, 2023, it was reported that Razer, a popular brand in the premium gaming laptop industry, is offering an exceptional deal on their Razer Blade 17 model. Typically

Innovation Ignition

New Fintech Innovation Ignites Change

The fintech sector continues to attract substantial interest, as demonstrated by a dedicated fintech stage at a recent event featuring panel discussions and informal conversations with industry professionals. The gathering,

Import Easing

Easing Import Rules for Big Tech

India has chosen to ease its proposed restrictions on imports of laptops, tablets, and other IT hardware, allowing manufacturers like Apple Inc., HP Inc., and Dell Technologies Inc. more time

Semiconductor Stock Plummet

Dramatic Downturn in Semiconductor Stocks Looms

Recent events show that the S&P Semiconductors Select Industry Index seems to be experiencing a downturn, which could result in a decline in semiconductor stocks. Known as a key indicator

Anthropic Investment

Amazon’s Bold Anthropic Investment

On Monday, Amazon announced its plan to invest up to $4 billion in the AI firm Anthropic, acquiring a minority stake in the process. This decision demonstrates Amazon’s commitment to

AI Experts Get Hired

Tech Industry Rehiring Wave: AI Experts Wanted

A few months ago, Big Tech companies were downsizing their workforce, but currently, many are considering rehiring some of these employees, especially in popular fields such as artificial intelligence. The

Lagos Migration

Middle-Class Migration: Undermining Democracy?

As the middle class in Lagos, Nigeria, increasingly migrates to private communities, a PhD scholar from a leading technology institute has been investigating the impact of this development on democratic

AI Software Development

ChatGPT is Now Making Video Games

Pietro Schirano’s foray into using ChatGPT, an AI tool for programming, has opened up new vistas in game and software development. As design lead at business finance firm Brex, Schirano

Llama Codebot

Developers! Here’s Your Chatbot

Meta Platforms has recently unveiled Code Llama, a free chatbot designed to aid developers in crafting coding scripts. This large language model (LLM), developed using Meta’s Llama 2 model, serves

Tech Layoffs

Unraveling the Tech Sector’s Historic Job Losses

Throughout 2023, the tech sector has experienced a record-breaking number of job losses, impacting tens of thousands of workers across various companies, including well-established corporations and emerging startups in areas

Chinese 5G Limitation

Germany Considers Limiting Chinese 5G Tech

A recent report has put forth the possibility that Germany’s Federal Ministry of the Interior and Community may consider limiting the use of Chinese 5G technology by local network providers

Modern Warfare

The Barak Tank is Transforming Modern Warfare

The Barak tank is a groundbreaking addition to the Israeli Defense Forces’ arsenal, significantly enhancing their combat capabilities. This AI-powered military vehicle is expected to transform the way modern warfare

AI Cheating Growth

AI Plagiarism Challenges Shake Academic Integrity

As generative AI technologies like ChatGPT become increasingly prevalent among students and raise concerns about widespread cheating, prominent universities have halted their use of AI detection software, such as Turnitin’s

US Commitment

US Approves Sustainable Battery Research

The US Department of Energy has revealed a $325 million commitment in the research of innovative battery types, designed to enable solar and wind power as continuous, 24-hour energy sources.

Netanyahu Musk AI

Netanyahu and Musk Discuss AI Future

On September 22, 2023, Israeli Prime Minister Benjamin Netanyahu met with entrepreneur Elon Musk in San Francisco prior to attending the United Nations. In a live-streamed discussion, Netanyahu lauded Musk

Urban Gardening

Creating Thriving Cities Through Urban Gardening

The rising popularity of urban gardening is receiving increased recognition for its numerous advantages, as demonstrated in a recent study featured in the Environmental Research Letters journal. Carried out by

What You Need to Know About Cloud Security Strategies

What You Need to Know About Cloud Security Strategies

Today, many businesses are adopting cloud computing services. As a result, it’s important to recognize that security measures for data in the cloud are different from those in traditional on-premises

Romanian Energy Security

Eastern Europe is Achieving Energy Security

Canada and Romania have solidified their commitment to energy security and independence from Russian energy exports by signing a $3-billion export development agreement. The deal is centered on constructing two

Seamless Integration

Unlocking Seamless Smart Home Integration

The vision of an intelligently organized and interconnected smart home that conserves time, energy, and resources has long been desired by many homeowners. However, this aspiration has often been hindered

New Algorithm

MicroAlgo’s Groundbreaking Algorithm

MicroAlgo Inc. has revealed the creation of a knowledge-augmented backtracking search algorithm, developed through extensive research in evolutionary computational techniques. The algorithm is designed to boost problem-solving effectiveness, precision, and

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,