Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Event Handling in Android

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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.


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.


  1. Go to Android Studio IDE and create an empty application, and select the language as Java.
  2. Write the codes as given below for the given files i.e.,, src/main/AndroidManifest.xml, src/main/res/values/colors.xml, src/main/res/values/strings.xml, src/main/res/values/themes.xml
  3. Run the application to see the results on the emulator.

Code for


package com.example.eventhandler;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    // defining variables of type ImageButton
    ImageButton ib1, ib2;       
    protected void onCreate(Bundle savedInstanceState)
        // associating the variables with respective views
        ib1 = (ImageButton)findViewById(;  
        ib2 = (ImageButton)findViewById(;
          // 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(
                    // Changing the color of the text
        // 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(
                    // Changing the color of the text

Code for src/main/AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
                android:value="" />

Code for src/main/res/values/colors.xml:


<?xml version="1.0" encoding="utf-8"?>
    <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>

Code for src/main/res/values/strings.xml:


    <string name="app_name">GFG | First App</string>

Code for src/main/res/values/themes.xml:


<resources xmlns: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. -->

Now Simply run the application and click the red and green buttons and observe the output. 


My Personal Notes arrow_drop_up
Last Updated : 31 Oct, 2022
Like Article
Save Article
Similar Reads
Related Tutorials