Different way to handle events in Android

Typically events do responds to user interactions. Android platform supports multiple ways to handle the events on View’s. When user clicks on an Android View, some method is getting called by the android framework and then passed the control to the application listeners.

For example, when a user clicks on a view such as a Button, the onTouchEvent() method is called on that Button object. In order to make our application to responds to the event we must extend the class and override the method. But extending every View object in order to handle such an event would not be practical. Each View classes in Android provide collection of nested interfaces called listeners with callbacks that you can much more easily define in order to handle event.

1. Defining a listener programatically on the OnCreate method

button.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        //do stuff
    }
});

This way will create anonymous clases as much as you create buttons. This is recommended only if you have fewer listener in your class. But if we have a complex screen layout with many view’s then writing a listener programatically for each view will make the code messy. Its less readable and costly

2. Setting the android:OnClick property on the XML

<Button android:id="@+id/btnView"
    ...............
    ...............
    android:OnClick="btnViewOnClick"/>

Many people use this way of handling the click events by writing OnClick attribute in XML. But, usually it is not preferable as I because better to keep listeners inside code. Internally android is using java reflection concept behind the scene to handle this. It is less readable, and confuses other developers.

3. Implementing the OnClickListener interface on the Activity class and passing reference to the Button

public class MainActivity extends Activity implements OnClickListener{
    @Override
    public void onClick(View v) {
        //do stuff
    }

    protected void onCreate(Bundle savedInstanceState) {
        ...
        button.setOnClickListener(this);
    }
}

Here we are implementing the OnClickListener interface on the Activity class and passing a self reference to the Button. This way the onclick listener will hold the reference to the activity object, and so it is a heavy operation to keep the whole activity’s object in it.

This way we can handle the click event for all views. However, we need to differentiate view’s using their id’s. We can use view.getId() method to see which button was clicked. Again, this is preferable only when we have fewer views to handle. This way all the click event handling codes are done at one place.

This way is hard to navigate through, because you can’t determine the type of the listener you are using with current button (I know eclipse will highlight the methods this are pointing at, but with huge code I think it will be hard to find).

4. Create a field with the OnClickListener type

private OnClickListener onClickHandler = new OnClickListener(){

    @Override
    public void onClick(View v) {
        //stuff
    }
};

protected void onCreate(Bundle savedInstanceState) {
    ...
    button.setOnClickListener(onClickHandler);
}

The best practice is the create a local variable with the OnClickListener type. This way it is easy to navigate and more readable. But it doesn’t stop you to implement the other three options provided above. Just that every one has different way of looking at the problem.

Last updated by at .

Share this post:

PinIt

About Nilanchala

A blogger, bit of a tech freak and a software developer. He is working around smart phone arena with technologies like android, blackberry, PhoneGap and Sencha Touch. +1 on Google+

  • george

    Very useful I think

  • http://marki-tos.tumblr.com/ Todo poderoso

    ok nice!