1

So what's the difference between setting a button listener like this :

private OnClickListener myButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
          //do stuff
     }
}

And putting the name of a method on android:onclick attribute-

android:onClick="onClickMyButton"

Then in the activity class add the said method.

public void onClickMyButton(View v) {
  // do stuff

}

Most pieces of code I've seen use the first approach, I tend to use the latter, is there any difference ?

Salim Mahboubi
  • 561
  • 7
  • 25

1 Answers1

1

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.

HassanUsman
  • 1,787
  • 1
  • 20
  • 38