34,333
edits
Changes
no edit summary
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Android Touch and Multi-touch Event Handling|Previous]]<td align="center">[[Kindle Fire Android 4 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[Implementing Android Custom Gesture and Pinch Recognition|Next]]</td>
<tr>
<td width="20%">Android Touch and Multi-touch Event Handling<td align="center"><td width="20%" align="right">Implementing Android Custom Gesture and Pinch Recognition</td>
The Android SDK provides mechanisms for the detection of both common and custom gestures within an application. Common gestures involve interactions such as a tap, double tap, long press or a swiping motion in either a horizontal or a vertical direction (referred to in Android nomenclature as a fling).
The goal of this chapter is to explore the use of the Android GestureDetector class to detect common gestures performed on the display of a Kindle Fire an Android device. The next chapter, entitled [[Implementing Android Custom Gesture and Pinch Recognition on the Kindle Fire]], will cover the detection of more complex, custom gestures such as circular motions and pinches.
== Implementing Common Gesture Detection ==
When a user interacts with the display of a Kindle Fire an Android device, the onTouchEvent() method of the currently active application is called by the system and passed MotionEvent objects containing data about the user’s contact with the screen. This data can be interpreted to identify if the motion on the screen matches a common gesture such as a tap or a swipe. This can be achieved with very little programming effort by making use of the Android GestureDetectorCompat class. This class is designed specifically to receive motion event information from the application and to trigger method calls based on the type of common gesture, if any, detected.
The basic steps in detecting common gestures are as follows:
== Implementing the onTouchEvent() Method ==
If the application was to be compiled and run at this point, nothing would happen if gestures were performed on the Kindle Fire device display. This is because no code has been added to intercept touch events and to pass them through to the GestureDetectorCompat instance. In order to achieve this, it is necessary to override the onTouchEvent() method within the activity class and implement it such that it calls the onTouchEvent() method of the GestureDetectorCompat instance.
Remaining in the CommonGesturesActivity.java file, therefore, implement this method so that it reads as follows:
== Testing the Application ==
Compile and run the application on either a physical Kindle Fire Android device or an AVD emulator. Once launched, experiment with swipes, presses, scrolling motions and double and single taps. Note that the text view updates to reflect the events as illustrated in Figure 18 1:
<google>BUY_KINDLE_FIREBUY_ANDROID</google>
<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Android Touch and Multi-touch Event Handling|Previous]]<td align="center">[[Kindle Fire Android 4 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[Implementing Android Custom Gesture and Pinch Recognition|Next]]</td>
<tr>
<td width="20%">Android Touch and Multi-touch Event Handling<td align="center"><td width="20%" align="right">Implementing Android Custom Gesture and Pinch Recognition</td>
</table>