© 2013

Apps Maven - The Technology Experts

Event handling involves the study of the triggering mechanism of an underlying activity to carry out the task performance. Out of a variety of forms of an android event, it generates a response to external action. One such common example of input events includes user interaction with the mobile touch screen.

Android Events
There is an event queue in the android framework which operates First-in, First-out principle on the occurring events. For instance, in an input event of touch on the phone screen, the event is forwarded to the view that is positioned at the screen location where touch action originated. In the case of event notification, the view also receives the nature of the event. To handle every event that has been passed to the view, it must have an event listener.

Let us throw light on the three most important concepts in the context of Android Event Management:
Event Listener: The view class has an interface called an event listener consisting of a single callback method. The android framework calls these methods when the view that is reported with the listener is activated or triggered by user interaction.

Let us know about a few of the important event listeners such as:
onClick(): It is invoked when the user focuses or touches or clicks on a button, images or text like widgets.
onLongClick(): When the user presses or focuses on widgets like buttons or text for more than one or two seconds, this method is invoked.
onKey(): It is invoked when the user focuses on the item and presses the device’s hardware key.
onCreateContextMenu(): It is called when the context menu is being created(in the response of the sustained long press).
onMenuItemClick(): It is invoked when a user selects an item on the menu.
Event Handlers: It is called by the event listener (that is registered for the event) to handle the event efficiently.

Some of the important event handlers are as follows:
OnKeyUp(): The system calls this method when a new event takes place.
onKeyDown(): The system calls this method in the occurrence of a key down event.
onTouchEvent(): The system calls this method when any touch event is noticed such as pressing a key, releasing a key or any gesture of movement on the screen.
onTrackballEvent(): The system calls this method in case of a trackball motion event. 
OnFocusChange(): This method is invoked when a widget or an event loses its focus or gets into focus/highlight. For example- when a user moves away from the view.
There are many other event handlers that may be important for an application such as onClickListener, onDragListener, etc. For the purpose of the development of sophisticated apps, refer to the android app development’s official documentation.
Event Listeners Registration: It is a process to register an event listener with an event handler so that whenever any event is fired by the event listener, the event handler can be called to work.

The registration of event listeners can be done in the following ways:
Event listeners can be directly mentioned in activity_main.xml to follow the first method of its registration.
We can make use of Activity class in order to register an event listener as it implements the listener interface.
Event listeners can also be registered by using an anonymous inner class.

We will discuss below the above mentioned three scenarios in detail:
Touch mode: The users tend to interact with their devices by either touching the screen or using the hardware keys. So, it becomes necessary to pay attention to the actionable items. When a device with touch functionality enters the touch mode since then actionable icons are kept in focus like a home button or navigation button.

Focus handling: As per user input, the android framework handles the focus movement that involves addition and removal of focus on the basis of input movements performed by the user. With the help of isFocusable(), we can ensure if the view is in focusable mode or not and with setFocusable(), we can alter the focus mode successfully.

Event Handling Implementation: Moving towards the implementation of android event handling we will learn the way it functions.
First Step: Open the tab of the New Project and fill up all the details that are required.
Second Step: Open your project followed by our layout setup and the designated Id of the elements so that they can be invoked easily.
Third Step: Open the file of activity_main.xml and write the required code.
Fourth Step: After completing the code of activity.xml, proceed with the main_activity.java code.
Fifth Step: Furthermore, open color.xml by following the order like
App – src – main – res – values - color.xml
In conclusion, you can run the application happily after writing the above codes.

Examples of Event Handling
The registration of event listeners by using an anonymous inner class is one such example of event handling. It will prove useful to build an anonymous implementation of the listener when each class is applied to only one control if there is an added advantage of passing arguments to the event handler. This approach will not require any reference to call Activity as all its private data can be accessed by the event handler itself.
But in case, the handler is applied to multiple controls, you have to perform cut and paste operation for the handler and the maintenance of code in such a scenario becomes a difficult task if it is long enough.

It is very important to understand the concept of android event handling because there is no widespread use of a user interface if it is not capable of responding to user interaction. With the basic understanding of event listeners and callback methods, android is filling the gaps between the application’s back-end code and the user interface.
You can try different types of event handlers on your own to understand the type of events they are concerned to handle. I have laid emphasis on the main concepts of event handling as well as callback methods. Feel free to share your feedback and drop in your views.