Module 7: Event Handling in Android Studio: Gordon College
Module 7: Event Handling in Android Studio: Gordon College
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Module 7 will discusses Events and how it is being handled in Android Studio.
In Android Application event refers to an action that can be done by the user
within the interface known as Activity.
It is very important in collecting information from the user. These events can
also be used to trigger and initiate functionalities after the user press, touch or
even type something on the user interface of an Android Application.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 1 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
On Android, there's more than one way to intercept the events from a user's
interaction with your application. When considering events within your user
interface, the approach is to capture the events from the specific View object
that the user interacts with. The View class provides the means to do so.
Within the various View classes that you'll use to compose your layout, you may
notice several public callback methods that look useful for UI events. These
methods are called by the Android framework when the respective action occurs
on that object. For instance, when a View (such as a Button) is touched, the
onTouchEvent() method is called on that object. However, in order to intercept
this, you must extend the class and override the method. However, extending
every View object in order to handle such an event would not be practical. This
is why the View class also contains a collection of nested interfaces with
callbacks that you can much more easily define. These interfaces, called event
listeners, are your ticket to capturing the user interaction with your UI.
While you will more commonly use the event listeners to listen for user
interaction, there may come a time when you do want to extend a View class, in
order to build a custom component. Perhaps you want to extend the Button
class to make something more fancy. In this case, you'll be able to define the
default event behaviors for your class using the class event handlers.
C. Event Listeners
An event listener is an interface in the View class that contains a single callback
method. These methods will be called by the Android framework when the View
to which the listener has been registered is triggered by user interaction with the
item in the UI.
Included in the event listener interfaces are the following callback methods:
C.1. onClick() - From View.OnClickListener. This is called when the user either
touches the item (when in touch mode), or focuses upon the item with the
navigation-keys or trackball and presses the suitable "enter" key or presses
down on the trackball.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 2 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
These methods are the sole inhabitants of their respective interface. To define
one of these methods and handle your events, implement the nested interface in
your Activity or define it as an anonymous class. Then, pass an instance of your
implementation to the respective View.set...Listener() method. (E.g., call
setOnClickListener() and pass it your implementation of the OnClickListener.)
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 3 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
Notice that the onClick() callback in the above example has no return value, but
some other event listener methods must return a boolean. The reason depends
on the event. For the few that do, here's why:
Remember that hardware key events are always delivered to the View
currently in focus. They are dispatched starting from the top of the View
hierarchy, and then down, until they reach the appropriate destination.
If your View (or a child of your View) currently has focus, then you can
see the event travel through the dispatchKeyEvent() method. As an
alternative to capturing key events through your View, you can also
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 4 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
receive all of the events inside your Activity with onKeyDown() and
onKeyUp().
Also, when thinking about text input for your application, remember that
many devices only have software input methods. Such methods are not
required to be key-based; some may use voice input, handwriting, and so
on. Even if an input method presents a keyboard-like interface, it will
generally not trigger the onKeyDown() family of events. You should never
build a UI that requires specific key presses to be controlled unless you
want to limit your application to devices with a hardware keyboard. In
particular, do not rely on these methods to validate input when the user
presses the return key; instead, use actions like IME_ACTION_DONE to
signal the input method how your application expects to react, so it may
change its UI in a meaningful way. Avoid assumptions about how a
software input method should work and just trust it to supply already
formatted text to your application.
E. Event Handlers
If you're building a custom component from View, then you'll be able to define
several callback methods used as default event handlers. In Custom View
Components, there are some of the common callbacks used for event handling,
including:
• onKeyDown(int, KeyEvent) - Called when a new key event occurs.
• onKeyUp(int, KeyEvent) - Called when a key up event occurs.
• onTrackballEvent(MotionEvent) - Called when a trackball motion event
occurs.
• onTouchEvent(MotionEvent) - Called when a touch screen motion
event occurs.
• onFocusChanged(boolean, int, Rect) - Called when the view gains or
loses focus.
There are some other methods that you should be aware of, which are not part
of the View class, but can directly impact the way you're able to handle events.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 5 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
So, when managing more complex events inside a layout, consider these other
methods:
• Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity
to intercept all touch events before they are dispatched to the window.
F. Touchmode
When a user is navigating a user interface with directional keys or a trackball, it
is necessary to give focus to actionable items (like buttons) so the user can see
what will accept input. If the device has touch capabilities, however, and the
user begins interacting with the interface by touching it, then it is no longer
necessary to highlight items, or give focus to a particular View. Thus, there is a
mode for interaction named "touch mode."
For a touch-capable device, once the user touches the screen, the device will
enter touch mode. From this point onward, only Views for which
isFocusableInTouchMode() is true will be focusable, such as text editing widgets.
Other Views that are touchable, like buttons, will not take focus when touched;
they will simply fire their on-click listeners when pressed.
Any time a user hits a directional key or scrolls with a trackball, the device will
exit touch mode, and find a view to take focus. Now, the user may resume
interacting with the user interface without touching the screen.
The touch mode state is maintained throughout the entire system (all windows
and activities). To query the current state, you can call isInTouchMode() to see
whether the device is currently in touch mode.
G. Handling Focus
The framework will handle routine focus movement in response to user input.
This includes changing the focus as Views are removed or hidden, or as new
Views become available. Views indicate their willingness to take focus through
the isFocusable() method. To change whether a View can take focus, call
setFocusable(). When in touch mode, you may query whether a View allows
focus with isFocusableInTouchMode(). You can change this with
setFocusableInTouchMode().
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 6 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
<LinearLayout
android:orientation="vertical"
... >
<Button android:id="@+id/top"
android:nextFocusUp="@+id/bottom"
... />
<Button android:id="@+id/bottom"
android:nextFocusDown="@+id/top"
... />
</LinearLayout>
Ordinarily, in this vertical layout, navigating up from the first Button would not
go anywhere, nor would navigating down from the second Button. Now that the
top Button has defined the bottom one as the nextFocusUp (and vice versa), the
navigation focus will cycle from top-to-bottom and bottom-to-top.
To listen for focus events (be notified when a View receives or loses focus), use
onFocusChange(), as discussed in the Event listeners section.
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 7 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
V. LEARNING TASK
Holistic Rubric
Exceeding Meeting Approaching Below None
(10) (8) (6) (2) (0)
Substantial,
Content Clarity
specific, and/or
The presence of ideas Sufficient
illustrative
developed through developed Limited content Superficial
content No content
facts, examples, content with with inadequate and/or
demonstrating or answer
illustrations, details, adequate elaboration or minimal
strong provided.
opinions, statistics, elaboration or explanation content
development and
reasons, and/or explanation.
sophisticated
explanations.
ideas.
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
Score Sheet
Question 1 Question 2 Question 3 Total Score
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 8 of 9
Republic of the Philippines
City of Olongapo
-o0o-
GORDON COLLEGE
Olongapo City Sports Complex, Donor St., East Tapinac, Olongapo City 2200
www.gordoncollege.edu.ph
VI. REFERENCES
Online Resources
• https://www.tutorialspoint.com/android/android_event_handling.htm
• https://developer.android.com/guide/topics/ui/ui-events
• https://www.tutlane.com/tutorial/android/android-input-events-event-
listeners-event-handling
• https://javatutorial.net/event-handing-android
IT Elective II (Lec) NOT FOR SALE, Exclusive for Gordon College Only!
Neil Marc R. Biron, Instructor (1st Semester, AY 2020-2021) | 9 of 9