Sarthak Mittal Sarthak Mittal - 6 months ago 53
Android Question

Best way to implement View.OnClickListener in android

Suppose we have an Activity with a lot of views on which

OnClickListener
is to be registered.

The most common way to implement this is to let the Activity-Subclass implement the OnClickListener, something like this:

public class ActivityMain extends Activity implements View.OnClickListener
{
@Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
}
}
}


The way i like to implement it is to create a private class inside the Activity-Subclass and let that inner class
implement the OnClickListener:

public class ActivityMain extends Activity implements View.OnClickListener
{
private class ClickListener implements View.OnClickListener
{
@Override
public void onClick(View view)
{
switch (view.getId())
{
//handle multiple view click events
}
}
}
}


This way the code seems more organized and easy to maintain.

Moreover, talking about "Is-a", "Has-a" relationships, the latter seems to be a good practice because now the
Activity-Subclass would have a "Has-a" relationship with the ClickListener.
While in the former method we would be saying that Our Activity-Subclass "Is-a" ClickListener, which ain't completely true.

Note that, i am not concerned with the memory overhead the latter would cause.

Also, adding onClick tag in xml is completely out of question.

So, what really is the best way to implement a ClickListener?

Please don't suggest any libraries like RoboGuice or ButterKnife etc.

Answer

Firstly there is no best practice defined by android regarding registering click listener. It Totally depends on your use case.

Implementing the View.OnClickListener interface to activity is the way to go. As Android strongly recommends interface implementation over and over again whether its is a activity or fragment.

Now as you described :

public class ActivityMain extends Activity implements View.OnClickListener
{
    private class ClickListener implements View.OnClickListener
    {   
        @Override
        public void onClick(View view)
        {
            switch (view.getId())
            {
                //handle multiple view click events
            }
        }
    }
}

This is your approach.Now it is your way of implementation nothing wrong with this if you are not concerned with memory overhead.But whats the benefit of creating the inner class and implementing the View.OnClickListener if you can simply implement that to main class which can also lead to code clarity and simplicity that you need.

So It just an discussion rather getting the best possible solution of implemeting the View.OnClickListener because if you go from practical point of everyone, will go for a solution which is simple and memory efficent.

So i would prefer the conventional way, keep things simple and efficient as below :

@Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            //handle multiple view click events
        }
    }

P.S : Your approach will defintely increase LOC :P ;)