<table border="0" cellspacing="0" width="100%">
<tr>
<td width="20%">[[Creating an Android User Interface in Java Code|Previous]]<td align="center">[[Kindle Fire Android 4 App Development Essentials|Table of Contents]]<td width="20%" align="right">[[Android Touch and Multi-touch Event Handling|Next]]</td>
<tr>
<td width="20%">Creating an Android User Interface in Java Code<td align="center"><td width="20%" align="right">Android Touch and Multi-touch Event Handling</td>
<google>BUY_KINDLE_FIREBUY_ANDROID</google>
Much has been covered in the previous chapters relating to the design of user interfaces for Kindle Fire based fo Android applications. An area that has yet to be covered, however, involves the way in which a user’s interaction with the user interface triggers the underlying activity to perform a task. In other words, we know from the previous chapters how to create a user interface containing a button view, but not how to make something happen within the application when it is touched by the user.
The primary objective of this chapter, therefore, is to provide an overview of event handling in Android applications. Once the basics of event handling have been covered, the next chapter will cover touch event handling in terms of detecting multiple touches and touch motion.
== Understanding Android Events ==
Events in Android can take a variety of different forms, but are usually generated in response to an external action. The most common form of events, particularly for devices such as the Kindle Firetablets and smartphones, involve some form of interaction with the touch screen. Such events fall into the category of input events.
The Android framework maintains an event queue into which events are placed as they occur. Events are then removed from the queue on a first-in, first-out (FIFO) basis. In the case of an input event such as a touch on the screen, the event is passed to the view positioned at the location on the screen where the touch took place. In addition to the event notification, the view is also passed a range of information (depending on the event type) about the nature of the event such as the coordinates of the point of contact between the user’s fingertip and the screen.
== Event Listeners and Callback Methods ==
In the example activity outlined later in this chapter the steps involved in registering an event listener and implementing the callback method will be covered in detail. Before doing so, however, it is worth taking some time to outline the event listeners that are available in the Android framework and the callback methods associated with each one. Note that some of these listeners respond to hardware features not present on the Kindle Fire device.
* '''onClickListener''' – Used to detect click style events whereby the user touches and then releases an area of the Kindle Fire device display occupied by a view. Corresponds to the onClick() callback method which is passed a reference to the view that received the event as an argument.
* '''onLongClickListener''' – Used to detect when the user maintains the touch over a view for an extended period. Corresponds to the onLongClick() callback method which is passed the view that received the event as an argument.
* '''onTouchListener''' – Used to detect any form of contact with the touch screen including individual or multiple touches and gesture motions. Corresponding with the onTouch() callback, this topic will be covered in greater detail in the chapter entitled Android Touch and Multi-touch Event Handling. The callback method is passed the view that received the event and a MotionEvent object as arguments.
* '''onCreateContextMenuListener''' – Listens for the creation of a context menu as the result of a long click. Corresponds to the onCreateContextMenu() callback method. The callback is passed the menu, the view that received the event and a menu context object.
Exception encountered, of type "Error"
[41be928b] /index.php/Special:MobileDiff/15674 Error from line 434 of /home/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /home/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /home/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /home/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /home/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /home/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /home/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /home/techotopia/extensions/MobileFrontend/includes/diff/InlineDiffFormatter.php(99): WordLevelDiff->__construct()
#7 /home/techotopia/includes/diff/DiffFormatter.php(140): InlineDiffFormatter->changed()
#8 /home/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /home/techotopia/extensions/MobileFrontend/includes/diff/InlineDifferenceEngine.php(117): DiffFormatter->format()
#10 /home/techotopia/includes/diff/DifferenceEngine.php(797): InlineDifferenceEngine->generateTextDiffBody()
#11 /home/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /home/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(241): DifferenceEngine->getDiffBody()
#13 /home/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(135): SpecialMobileDiff->showDiff()
#14 /home/techotopia/extensions/MobileFrontend/includes/specials/MobileSpecialPage.php(53): SpecialMobileDiff->executeWhenAvailable()
#15 /home/techotopia/includes/specialpage/SpecialPage.php(384): MobileSpecialPage->execute()
#16 /home/techotopia/includes/specialpage/SpecialPageFactory.php(553): SpecialPage->run()
#17 /home/techotopia/includes/MediaWiki.php(281): SpecialPageFactory::executePath()
#18 /home/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#19 /home/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#20 /home/techotopia/index.php(41): MediaWiki->run()
#21 {main}