Difference between OnClick() event and OnClickListener?

24,894

Solution 1

I'm not sure the question is clear. View.OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.

Solution 2

OnClickListener is the interface you need to implement and can be set to a view in java code.

Lately android added a xml attribute to views called android:onclick, that can be used to handle clicks directly in the view's activity without need to implement any interface.

Both function the same way, just that one gets set through java code and the other through xml code.

Solution 3

I am assuming by onClick that you use is the one that you defines in XML Layout. These two are alternative that serve same function but implemented differently.

  1. The onClick with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (the View) in order for onClick to function.

  2. An OnClickListener is an interface that any class could implement. Since it is an interface that any class could implement, this has more flexibility and more complex in its form. Few flexibilities that you could have with OnClickListener

    • You could easily swap one listener implementation with another if you need to.
    • An OnClickListener enable you to separate the action/behavior of the click event from the View that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the code
    • Since OnClickListener is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event. Again, this is not a big deal in simple cases, but for complex cases, we don't want to necessary mix up the variables/methods that related to event handling with the code of the View that triggers the event.

Solution 4

OnClickListener is what waits for someone to actually click, onclick determines what happens when someone clicks

the listener is a class, the onclick is a method, this distinction is not very useful in simple cases, but if you want to be more complicated it becomes more necessary

Solution 5

Button button = (Button)findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do stuff
    }
});

OnClickListener is an interface and onClick is method of OnClickListener.

Share:
24,894

Related videos on Youtube

Praveenkumar
Author by

Praveenkumar

https://stackoverflow.com/story/spraveenk91

Updated on September 25, 2020

Comments

  • Praveenkumar
    Praveenkumar almost 4 years

    I'm always using onclick() event in most of my projects. But, I read about OnClickListener(). Can anyone tell what's the difference between these two? And which one is best to use in Android application?.

    • Reno
      Reno almost 13 years
      You have accepted the wrong answer. Sean Owen or userSeven7s is correct.
  • Reno
    Reno almost 13 years
    So sad that there is a voting ring in android that down-votes the right answers :(

Related