Android Input Events (Event Listeners, Event Handling)

  Total Views : 209
  Comments : 0
  Keywords : Android input events such as event listeners and event handling is used to handle the event’s from a user interaction of our application, Android event listeners to capture the events

Here we will learn android input events such as event listeners and event handling with examples and how to use event listeners & event handling to capture the events from the specific view objects in user interface of application with examples.

Android Input Events

In android, Input Events are used to capture the events, such as button clicks, edittext touch, etc. from the View objects that defined in user interface of our application, when the user interacts with it.

 

To handle input events in android, the views must have in place an event listener. The View class, from which all UI components are derived contains a wide range event listener interfaces and each listener interface contains an abstract declaration for a callback method. To respond to an event of a particular type, the view must register an appropriate event listener and implement the corresponding callback method.

 

For example, if button is to respond to a click event it must register to View.onClickListener event listener and implement the corresponding onClick() callback method. In application when a button click event is detected, the Android framework will call the onClick() method of that particular view.

 

Generally, to handle input events we use Event Listeners and Event Handling in android applications to listen for user interactions and to extend a View class, in order to build a custom component.

Android Event Listeners

In android, Event Listener is an interface in the View class that contains a single call-back method. These methods will be called by the Android framework when the View which is registered with listener is triggered by user interaction with the item in UI.

 

Following are the call-back methods which included in event listener interface.

 

MethodDescription
onClick() This method is called when the user touches or focus on the item using navigation-keys or trackball or presses on "enter" key or presses down on the trackball.
onLongClick() This method is called when the user touches and holds the item or focuses on the item using navigation-keys or trackball and presses and holds "enter" key or presses and holds down on the trackball (for one second).
onFocusChange() This method is called when the user navigates onto or away from the item, using the navigation-keys or trackball.
onKey() This method is called when the user is focused on the item and presses or releases a hardware key on the device.
onTouch() This method is called when the user performs a touch event, including a press, a release, or any movement gesture on the screen.
onCreateContextMenu() This method is called when a Context Menu is being built.

 There are many more event listeners available as a part of View class to use it in our android applications.

Android Event Listeners Registration

In android, 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.

 

Following are the different ways to register event listeners in our android applications.

 

  • By specifying an event handlers directly in activity_main.xml file, we can register event listeners
  • By using Activity class that implements a listener interface, we can register event listeners
  • By using an anonymous class.

Following is the example of registering a Button onClick event listener in android application.

 

@Override
protected void onCreate(Bundle savedInstanceState) {
   
….
   
// Capture button from our layout
   
Button button = (Button)findViewById(R.id.btnShow);
   
// Register onClick listener with the below implementation
   
button.setOnClickListener(btnListener);

….
}
// Anonymous implementation of OnClickListener
private View.OnClickListener btnListener = new View.OnClickListener() {
   
public void onClick(View v) {
       
// do something when the button is clicked
   
}
};

This is how we can register our event listeners in different ways based on our requirements.

Android Event Handlers

In android, Event Handlers are useful to define a several callback methods when we are building a custom components from view.

 

Following are the some of commonly used callback methods for event handling in android applications.

 

MethodDescription
onKeyDown() This method is called when a new key event occurs.
onKeyUp() This method is called when a key up event occurs.
onTrackballEvent() This method is called when a trackball motion event occurs.
onTouchEvent() This method is called when a touch screen motion event occurs.
onFocusChanged() This method is called when the view gains or loses focus.

Now we will see how to define input Events using Even Listeners and Event Handling with examples in android applications.

Android Input Events Example

Following is the example of defining an input controls Button, TextView in user interface and showing the text in TextView when user click on Button in android application.

 

Create a new android application using android studio and give names as InputEventsExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

 

Now open an activity_main.xml file from \res\layout path and write the code like as shown below

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical" >
    <
Button
       
android:id="@+id/btnClick"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Click Event"
       
android:layout_marginTop="200dp" android:layout_marginLeft="130dp"/>
    <
TextView
       
android:id="@+id/txtResult"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:textColor="#86AD33"
       
android:textSize="20dp"
       
android:textStyle="bold"
       
android:layout_marginTop="12dp"/>
</
LinearLayout>

If you observe above code we created a one Button and TextView control in XML Layout file to show the text in textview when we click on Button

 

Once we are done with creation of layout with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.inputeventsexample path and write the code like as shown below.

MainActivity.java

package com.tutlane.inputeventsexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    Button
btn;
    TextView
tView;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
btn = (Button)findViewById(R.id.btnClick);
       
tView = (TextView)findViewById(R.id.txtResult);
       
btn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
tView.setText("You Clicked On Button");
            }
        });
    }
}

If you observe above code we registered our Button click event using setOnClickListener event listener and binding a data to TextView control when we click on button.

 

Generally, during the launch of our activity, onCreate() callback method will be called by android framework to get the required layout for an activity.

Output of Android Input Events Example

When we run above example using android virtual device (AVD) we will get a result like as shown below

 

Android Input Events Example Result 

 

If you observe above result, we are getting binding a text to textview control on button click. This is how we can handle input events in android applications based on our requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User