Android Tutorial

Android Tutorial Android Toast Button Widget In Android Tutorial Imageview Widget In Android Android Date Picker Example Android Time Picker Example Android Operating System Android Activity Android Architecture Android Content Providers Android Edittext Android Emulator Android Fragments Android Gridview Android Broadcast Receivers Event Handling In Android What Is Android Studio Android Navigation Drawer Android Recyclerview Android Scrollview Android Navigation Android Operating System Android ListView Android Studio Resources Android Studio's Layout Editor Android TextView Android Styles and Themes How To Show Image On An Android Application How To Insert An Email In Android Application In Android Version 8 How To Insert The Mobile Number On An Android Application Using Edittext Difference between android developer and web AdapterViewFlipper in Android with Example Android 9.0 Pie vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android Project Folder Structure Assets folder in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development Android Navigation Application Components Difference between android developer and web developer Event Handling in Android How to reduce the size of APK in Android Top 7 Android App Development Books Top Programming Languages for Android Development AdapterViewFlipper in Android with Example Android 9.0 “Pie” vs. Android 10, which one is better Android Development or Web Development which one is the best to choose Android EditText Android Fragments How to Update Gradle in Android Studio Navigation Drawer ScrollView What is Android Studio? Android Architecture Android Content Providers

Event Handling in Android

Events are a convenient way to gather data about user interactions with interactive elements of your application. Like a button click or a screen touch. A queue of events on a first-in-first-out (FIFO) criteria is maintained by Android Framework. Your program can capture these events and take suitable action based on your needs.

There are 3important things associated with managing Android events:

  • Event listener - An event listener is an interface of the View class with one callback method. The Android framework calls these methods when the View in which the listener is registered is triggered by the interaction between the UI item and the user.
  • Registering an event listener-Event registration is a procedure in which aneventhandler gets registered with the listenerso that it is called when an event listener raises an event.
  • Event Handler-When an event occurs and you register an event listener for the event, the respective event handler is called by the event listener, which is the method that handles the event.

Event Listeners

1. OnClickListener()

This listener is called when the user either taps or touches or focuses upon any view such asImagebutton, card view, image, etc. To handle this event, you can use the onClick() event handler.

2. OnLongClickListener()

This listener is called when the user either focuses or touches or taps upon any view such as button, card view, Imagebutton,etc. for a short durationlike2to3 seconds. To handle this event, you can use onLongClick() event handler.

3. OnFocusChangeListener()

This listener is called when the view loses its focus i.e., the user stops interacting with the view item. To handle this event, you can use the onFocusChange() event handler.

4. OnFocusChangeListener()

This listener is called when the user is focusing on the view and presses or releases a hardware key on the device. To handle this event, you can use onKey() event handler.

5. OnTouchListener()

This listener is called when the user tapsor releases the key, or any movement gesture is noticed on the device’s screen. To handle this event, you can use onTouch() event handler.

6. OnMenuItemClickListener()

This listener is called when the user interacts with a menu item.To handle this event, you can use onMenuItemClick() event handler.

There are many other event listeners that you can use as part of the View class, and you can use them according to your application needs such as OnHoverListener and OnDragListener. Therefore, it is recommended to refer to the official documentation of Android application development when developing sophisticated applications.

Event Listener registration

Eventregistration is the procedure by which an event handler registers with the listener, sothe handlercan be called when the event listener has fired an event. There are many complicated ways to register an event listener with an event, but I'm listing only three here and you may use any one of them depending on your situation.

  • Use of anonymous inner classes
  • Activity classes implement the listener interface.
  • Specify event handlers directly using the layout file activity_main.xml.

The following sections show detailed examples of three scenarios. 

Touch Mode

Users can operate the device using hardware buttonsor keyboard keys, or by tapping on the screen. The device is in touch mode when you use it by touching the screen. Then the user can touch and manipulate virtual buttons or images on the screen, etc. You may check that whether the android device is in touch mode or not by calling the isInTouchMode() method of the View class.

Focus

A view or widget typically displays a highlighting or blinking cursor when in focus. This indicates that you are ready to accept user input.

  • isFocusable()-Returns true or false.
  • isFocusableInTouchMode()-First, make sure that the view is in focus in touch mode. (You can focus on the display when using the hardware keys, but you can't focus when the device is in touch mode.)

Event Handling Example

Here is the main activity file src/com.example.application/ClickActivity.java content: This file contains each of the basic lifecycle methods.

public class ClickActivityextends AppCompatActivity{
int count = 0;
Button inc;
Button dec;
TextViewtxt;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);


txt = findViewById(R.id.countText);


inc= findViewById(R.id.button5);


inc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
                increment();
txt.setText(String.valueOf(count));
            }
        });


dec = findViewById(R.id.button6);
dec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
                decrement();
txt.setText(String.valueOf(count));
            }
        });
    }