2

Edit: No, this is not a duplicate for the given link is asking for the comparison of setOnClickListener and android:onClick. I'm not even asking for a comparison, but I'm asking about the advantage of having an implementation of View.OnClickListener.

Please be free to vote to re-open.


Many people, by preference, use

public class TrumpLocator extends Clinton implements View.onClickListener{
  @Override
  public void onClick(View v){
      //...
   }
}

However, if I'm not mistaken, either way, on your Button you have to do:

android:onClick="onClick"

But, again if I'm not mistaken, if we don't override onClick and implement View.onClickListener, we will achieve the same effect:

//no override and no "implements onClickListener"
public void onClick(View v){
  //...
}

and

android:onClick="onClick"

So, is there any advantage of implementing the method over simply applying the click listener? Isn't it just a waste of code?

Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
  • It's a matter of opinion. The Java code makes it more clear, IMO – OneCricketeer Nov 07 '16 at 17:33
  • @cricket_007 I am completely aware of that, but I'm making sure there aren't any advantages, since implementing seems very useless – Ali Bdeir Nov 07 '16 at 17:35
  • How are your Fragments handling the android:onClick ? – Ognian Gloushkov Nov 07 '16 at 17:36
  • @OgnianGloushkov by `setOnClickListener`. If we override onClick, then we can use the `android:onClick` with Fragments? – Ali Bdeir Nov 07 '16 at 17:37
  • @PavneetSingh no, it isn't a duplicate. That's the difference between android:onClick and setOnClickLister. Mine is what does implementing onCLickListener achieve. – Ali Bdeir Nov 07 '16 at 17:42
  • 1
    please study the answer in the linked question , it will provide the necessary details that you are seeking – Pavneet_Singh Nov 07 '16 at 17:47
  • @PavneetSingh I don't see anything, in a direct way, that says anything about `implements OnClickListener` and it's advantage over `onClick:` without the implementation. (Other than the fact that setOnClickListener itself overrides the method.) – Ali Bdeir Nov 07 '16 at 17:50
  • sometimes things can be subtle but in this scenario if you know what both things does then you can easily figure out the advantages but keep in mind the advantages and disadvantages are based on different-2 scenarios so if i want dynamism , if would go with `onclicklistener` otherwise `onclick` plus i can't mark a question as dupe myself, SO is community driven site, :) plus the linked question has necessary details to which can be considered as advantages of one over other – Pavneet_Singh Nov 07 '16 at 17:57

1 Answers1

3

either way, on your Button you have to do:

android:onClick="onClick"

No, this isn't required.

I think you have it backwards maybe. By having android:onClick="onClick", you need a public void method with that name in the quotes.

public void onClick(View v){
  //...
}

This is similar to implementing the interface, but not the exact same. In other words, it could just as well be android:onClick="handleClick", then you need

public void handleClick(View v){
  //...
}

People may not prefer this because it can lead to typos and uncertainty where a click listener is attached.


Now, the Activity does not need to implement the interface itself, you can attach anonymous class listeners to the views individually.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • So basically, if you implement it, you DON'T need a public method? – Ali Bdeir Nov 07 '16 at 17:43
  • If you implement an interface, you do have to implement it's methods. Otherwise the code does not compile. I'm saying the XML isn't required – OneCricketeer Nov 07 '16 at 17:44
  • So, when you're implementing `OnClickListener` you don't need to set the android:onClick in the XML? Never knew that. I must've been doing something wrong in this 2 years of Android programming... – Ali Bdeir Nov 07 '16 at 17:46
  • Please check my edit and reconsider the close vote as I'm not even asking for a comparison but an advantage of using this implementation. – Ali Bdeir Nov 07 '16 at 17:47
  • Go ahead and try it. The XML is completely optional. I'm not sure what takes precedence. – OneCricketeer Nov 07 '16 at 17:47
  • @AbAppletic as cricket has already supported you very well so you can still accept his answer :) +1 and there is no doubt that it's a good question +1 – Pavneet_Singh Nov 07 '16 at 18:01