Wednesday, July 6, 2016

Android Event Handling

Before going to Event Handling we have to know what is Event: “Event is changing the state of object such as click on button, selecting checkbox or screen touch etc.”

Android Framework maintains an event queue as First-in, First-out (FIFO) basis.



Flow Chart of Event, Event Listener and Event Handler

When event happens, we have to register an Event Listener for that Event and then that Event Listener calls the Event Handler for that Event, Event Handler is actually a method which handles the event.
NOTE: An Event Listener is a function that waits for an Event to occur.
Some of Event Handlers and Event Listeners


1.

Event Handler: onClick()
Event Listener: onClickListener()
Description: This is called when the user either touches the item (when in touch mode), or focuses upon the item such as Button, Text etc.


2.

Event Handler: onLongClick()
Event Listener: onLongClickListener()
Description: This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item (for one second).


3.

Event Handler: onFocusChange()
Event Listener: onFocusChangeListener()
Description: This is called when the user navigates onto or away from the item


4.

Event Handler: onKey()
Event Listener: onKeyListener()
Description: This is called when the user is focused on the item and presses or releases a hardware key on the device.


5.

Event Handler: onTouch()
Event Listener: onTouchListener()
Description:  This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).

6.

Event Handler: onCreateContextMenu()
Event Listener: onCreateContextMenuListener()
Description: This is called when a Context Menu is being built (as the result of a sustained "long click").

In Android to use Event Handler we have to register an Event Listener, there are many ways to register an Event Listener for an Event, but I will explain only top 2 ways. 



1.Using an Anonymous Inner Class

Here we will learn how to create an anonymous implementation of the listener.
It is harder to maintain code when we apply this approach to more than one control.

Working Example: 

Suppose we have a button (@id: button1), then to apply this approach to set onClickListener() on this button we will use following method.
public class MainActivity extends AppCompatActivity {
  @Override
    protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1 = (Button) findViewById(R.id.button1);
  
        button1.setOnClickListener(new View.OnClickListener() {
           @Override

            public void onClick(View v) {   // Code to be executed on Button Click
            }
       });
      }
}

2.Activity class implements the Listener interface

This method is useful when we have to apply Event Handler to multiple controls.

Working Example: 

Suppose we have a button (@id: button1), then to apply this approach to set onClickListener() on this button we will use following method.

In this approach, 
First we have to implement onClickListener on Activity class as shown below
Second apply set onClickListener to this button
Third create Override method onClick() and use switch to set function to button.

This is how finally code will look.

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button) findViewById(R.id.button1);
      
      button1.setOnClickListener(this);
    }
  
@Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button1: // Code to be executed on button click
                break;
            default:
                break;
        }
    } 
 }

If you have any query or doubt, contact me through contact form or through social media.