Event Handling
Android Event Handling
Events are an effective way to track and respond to user interactions with an application’s interactive components, such as button clicks, screen touches, key presses, and more.
The Android framework manages events using an event queue that follows the First-In, First-Out (FIFO) principle. Developers can listen to these events and perform actions based on application requirements.
Core Concepts of Android Event Management
Android event handling is mainly based on the following three concepts:
1. Event Listeners
An Event Listener is an interface defined in the View class that contains a callback method. The Android system automatically invokes this method when a user interacts with the UI element to which the listener is attached.
2. Event Listener Registration
Event registration refers to the process of attaching an event handler to a specific event listener. Once registered, the handler is triggered whenever the corresponding event occurs.
3. Event Handlers
An Event Handler is a method that executes when an event takes place. It contains the actual logic that responds to user actions.
Event Listeners and Event Handlers
| Event Handler | Event Listener | Description |
|---|---|---|
| onClick() | OnClickListener() | Triggered when the user clicks or taps a widget like a button, text, or image. |
| onLongClick() | OnLongClickListener() | Invoked when the user presses and holds a widget for a few seconds. |
| onFocusChange() | OnFocusChangeListener() | Called when a widget gains or loses focus. |
| onKey() | OnKeyListener() | Triggered when a hardware key is pressed or released while the view is focused. |
| onTouch() | OnTouchListener() | Called for touch events such as press, release, or movement on the screen. |
| onMenuItemClick() | OnMenuItemClickListener() | Fired when a menu item is selected. |
| onCreateContextMenu() | OnCreateContextMenuListener() | Invoked while creating a context menu after a long press. |
Android also provides many other listeners like OnHoverListener, OnDragListener, etc. For advanced use cases, referring to the official Android documentation is recommended.
Event Listener Registration Methods
Event registration links an event handler to an event listener. There are multiple ways to do this, but the most commonly used methods are:
Using an Anonymous Inner Class
Implementing the Listener Interface in the Activity class
Declaring the event handler directly in the XML layout file
Examples of these methods are discussed in the following sections.
Touch Mode
Users can interact with Android devices using hardware buttons or by touching the screen. When the screen is touched, the device enters touch mode, allowing interaction with on-screen components such as buttons and images.
You can check whether the device is in touch mode using:
isInTouchMode()
Focus Handling
A view is said to be in focus when it is highlighted or shows a blinking cursor, indicating it is ready to receive user input.
isFocusable() – Returns true if the view can gain focus
isFocusableInTouchMode() – Checks whether the view can be focused while in touch mode
Example attribute:
android:focusUp="@+id/button_l"
Handling Touch Events
public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()) { case TOUCH_DOWN: Toast.makeText(this, "You have pressed down the touch button", Toast.LENGTH_LONG).show(); break; case TOUCH_UP: Toast.makeText(this, "You have released the touch button", Toast.LENGTH_LONG).show(); break; case TOUCH_MOVE: Toast.makeText(this, "You are moving on the touch button", Toast.LENGTH_LONG).show(); break; } return super.onTouchEvent(event); }
Event Handling Example
Registering Event Listeners Using an Anonymous Inner Class
In this approach, an anonymous inner class is used to implement the listener. This method is useful when a listener is applied to only one control, and it allows access to the Activity’s private members.
However, if the same handler is applied to multiple controls, code duplication may occur, making maintenance difficult.
Steps to Implement Click Events
| Step | Description |
|---|---|
| 1 | Create a new Android application named myapplication using Android Studio. |
| 2 | Modify MainActivity.java to register click listeners for two buttons. |
| 3 | Update activity_main.xml to add UI components. |
| 4 | No need to define default string constants manually. |
| 5 | Run the application on the Android Emulator to test functionality. |
MainActivity.java
package com.example.myapplication; import android.app.ProgressDialog; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private ProgressDialog progress; Button b1, b2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = new ProgressDialog(this); b1 = (Button) findViewById(R.id.button); b2 = (Button) findViewById(R.id.button2); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView txtView = (TextView) findViewById(R.id.textView); txtView.setTextSize(25); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { TextView txtView = (TextView) findViewById(R.id.textView); txtView.setTextSize(55); } }); } }
activity_main.xml
AndroidManifest.xml
Application Output
When the app runs, clicking the Small Font or Large Font button changes the text size of the “Hello World” TextView. This happens because the registered click event handlers are executed on each button click.