Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement View.OnClickListener in android

Tags:

java

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.

UPDATE:

I would like to share the approach I finally adopted.

I directly implement the listener in Activity/Fragment.

As far as OOP design is concerned. The "HAS-A" approach doesn't offers any practical benefits and even takes up more memory. Considering the amount of nested classes (and the memory overhead) we will be creating for every similar listener we implement, this approach should clearly be avoided.

like image 302
Sarthak Mittal Avatar asked May 06 '15 16:05

Sarthak Mittal


People also ask

How do you implement setOnClickListener?

To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

Does Android have OnClickListener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

What is new view OnClickListener ()?

The onClickListener is constructed as: new View.OnClickListener() { public void onClick(View v) { } } That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.


1 Answers

First, there is no best practice defined by Android regarding registering click listeners. 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 it is an 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 and there is nothing wrong with this if you are not concerned with memory overhead. But what's the benefit of creating the inner class and implementing the View.OnClickListener if you can simply implement that in the main class which can also lead to the code clarity and simplicity that you need.

So it just a discussion rather getting the best possible solution of implementing the View.OnClickListener because if you go with the practical point of everyone, you will go for a solution which is simple and memory efficient.

So I would prefer the conventional way. It keeps things simple and efficient. Check the code below:

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

P.S : Your approach will definitely increase lines of code :P ;)

like image 152
kamal vaid Avatar answered Oct 05 '22 01:10

kamal vaid