Event Handling in Android
Last Updated :
26 Apr, 2025
Events are the actions performed by the user in order to interact with the application, for e.g. pressing a button or touching the screen. The events are managed by the android framework in the FIFO manner i.e. First In - First Out. Handling such actions or events by performing the desired task is called Event Handling.
Overview of the input event management
- Event Listeners: It is an interface in the View class. It contains a single callback method. Once the view to which the listener is associated is triggered due to user interaction, the callback methods are called.
- Event Handlers: It is responsible for dealing with the event that the event listeners registered for and performing the desired action for that respective event.
- Event Listeners Registration: Event Registration is the process in which an Event Handler gets associated with an Event Listener so that this handler is called when the respective Event Listener fires the event.
- Touch Mode: When using an app with physical keys it becomes necessary to give focus to buttons on which the user wants to perform the action but if the device is touch-enabled and the user interacts with the interface by touching it, then it is no longer necessary to highlight items or give focus to particular View. In such cases, the device enters touch mode and in such scenarios, only those views for which the isFocusableInTouchMode() is true will be focusable, e.g. plain text widget.
For e.g. if a button is pressed then this action or event gets registered by the event listener and then the task to be performed by that button press is handled by the event handler, it can be anything like changing the color of the text on a button press or changing the text itself, etc.
Event Listeners and their respective event handlers
- OnClickListener() - This method is called when the user clicks, touches, or focuses on any view (widget) like Button, ImageButton, Image, etc. Event handler used for this is onClick().
- OnLongClickListener() - This method is called when the user presses and holds a particular widget for one or more seconds. Event handler used for this is onLongClick().
- OnMenuItemClickListener() - This method is called when the user selects a menu item. Event handler used for this is onMenuItemClick().
- OnTouch() - This method is called either for a movement gesture on the screen or a press and release of an on-screen key. Event handler used for this is onTouch().
There are various other event listeners available which can be used for different requirements and can be found in the official documentation.
Implementation
Let us understand these concepts by an actual example. Here we will create an app with two Image Buttons where the red button turns the bottom text into red and the green button turns the text into green. A sample video is given below to get an idea about what we are going to do in this article.
Steps:
- Go to Android Studio IDE and create an empty application, and select the language as Java.
- Write the codes as given below for the given files i.e., MainActivity.java, src/main/AndroidManifest.xml, src/main/res/values/colors.xml, src/main/res/values/strings.xml, src/main/res/values/themes.xml
- Run the application to see the results on the emulator.
Code for MainActivity.java:
Java
package com.example.eventhandler;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// defining variables of type ImageButton
ImageButton ib1, ib2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// associating the variables with respective views
ib1 = (ImageButton)findViewById(R.id.imageButton6);
ib2 = (ImageButton)findViewById(R.id.imageButton7);
// Event listener for red button
ib1.setOnClickListener(new View.OnClickListener() {
// Event handler gets called for red button
// as soon listener registers the click
@Override public void onClick(View view)
{
TextView tv = (TextView)findViewById(
R.id.textView5);
tv.setTextColor(
// Changing the color of the text
getResources().getColor(R.color.red));
}
});
// Event listener for green button
ib2.setOnClickListener(new View.OnClickListener() {
// Event handler gets called for green button
// as soon listener registers the click
@Override public void onClick(View view)
{
TextView tv = (TextView)findViewById(
R.id.textView5);
tv.setTextColor(
// Changing the color of the text
getResources().getColor(R.color.green));
}
});
}
}
Code for src/main/AndroidManifest.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.EventHandler"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Code for src/main/res/values/colors.xml:
XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="green">#0F9D58</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FF0000</color>
</resources>
Code for src/main/res/values/strings.xml:
XML
<resources>
<string name="app_name">GFG | First App</string>
</resources>
Code for src/main/res/values/themes.xml:
XML
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.EventHandler" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/green</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
Now Simply run the application and click the red and green buttons and observe the output.
Output:
Similar Reads
Spring - Event Handling
Event handling in Spring is a powerful mechanism that allows different components of a Spring application to communicate with each other in a loosely coupled manner. This is achieved through the Observer Pattern, where a publisher sends events and multiple listeners react to them.Event Handling in S
5 min read
Event Handling in Java
An event is a change in the state of an object triggered by some action such as Clicking a button, Moving the cursor, Pressing a key on the keyboard, Scrolling a page, etc. In Java, the java.awt.event package provides various event classes to handle these actions.Classification of Events Events in J
5 min read
Input Events in Android with Example
In Android, there's quite a method to intercept the events from a user's interaction with your application. When considering events within your interface, the approach is to capture the events from the precise View object that the user interacts with. The View class provides the means to try to do s
7 min read
Handling Click Events in Button in Java Android
Click Events are one of the basic operations often used in Java Android Development to create Java Android Applications. In this article, we will learn about how to Handle Click Events in Button in Android Java.Methods to Handle Click Events in a ButtonThere are 2 ways to handle the click event in t
4 min read
C# - Handling an Event Declared in an Interface
Events and event handlers are generally used in case of a Publisher-Subscriber design pattern where some kind of event has occurred in a particular class and it needs to notify other classes about the event. The Publisher class has an event which gets invoked on some condition, the subscribers are n
4 min read
Android EditText in Kotlin
EditText is a widget in Android, that is used to get input from the user. EditText is commonly used in forms and login or registration screens. EditText extends the TextView class and provides more functionalities including handing text inputs such as cursor control, keyboard display and text valida
3 min read
How to Implement Polling in Android?
Many times you may have seen on some apps like youtube, LinkedIn, etc. polling is done and users choose their options whatever they want to choose. Here we are going to implement polling in Android Studio. What we are going to build in this article? In this article, we will ask the user a question a
4 min read
Introduction to Android Development
Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
DatePickerDialog in Android
Android DatePicker is a user interface control that is used to select the date by day, month, and year in the android application. DatePicker is used to ensure that the users will select a valid date. In android DatePicker having two modes, the first one shows the complete calendar and the second on
4 min read
Handle One Time Events in Android with Kotlin Channel
You must have used SingleLiveEvent class in android to get rid of multiple occurrences of SnackBars, Toasts, etc on changing device orientation (on rotating device)... If yes, then it's time to say goodbye to SingleLiveEvent Class. We now have a better solution to it using Kotlin's Channels. Before
4 min read