Thursday, 7 July 2016

Event Handling in Android

Event Handling in Android

Events are a useful way to collect data about a user's interaction with interactive components of Applications.Like button presses or screen touch etc. The Android framework maintains an event queue as first-in, first-out (FIFO) basis.

There are following three concepts related to Android Event Management −
  • 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.
  • Event Listeners Registration − Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event.
  • Event Handlers − When an event happens and we have registered an event listener for the event, the event listener calls the Event Handlers, which is the method that actually handles the event.

Event Listeners & Event Handlers

Event HandlerEvent Listener & Description
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event.
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event.
This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event.
This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event.
This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event.
This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.
This is called when the context menu is being built(as the result of a sustained "long click)

There are many more event listeners available as a part of View class like OnHoverListener, OnDragListener etc

Example of Button Click and LongClick

This example includes

 Event Handling in android

 Event Listener and call back method in android

 Add Multiple Event Listener in android

Start project

Create blank Activity named as Event2Activity
Button and textview placed on Activity window. File

package com.example.root.mysecondapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Event2Activity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        Toolbar toolbar = (Toolbar) findViewById(;

        FloatingActionButton fab = (FloatingActionButton) findViewById(;
        fab.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

        Button btnNewClick = (Button) findViewById(;
                new Button.OnClickListener(){
                    public void onClick(View v){
                        TextView txtHello = (TextView) findViewById(;
                        txtHello.setText("Hello Shaneesh");

                new Button.OnLongClickListener(){
                    public boolean onLongClick(View v){
                        TextView txtHello = (TextView) findViewById(;
                        txtHello.setText("Long Press Clicked!");
                        return true;


Touch Mode

Users can interact with their devices by using hardware keys or buttons or touching the screen.Touching the screen puts the device into touch mode. The user can then interact with it by touching the on-screen virtual buttons, images, etc.You can check if the device is in touch mode by calling the View class’s isInTouchMode() method.


  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article.
    Android Training in velachery | Android Training institute in chennai

  2. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training