How exactly does the android:onClick XML attribute differ from setOnClickListener?

From that I’ve read you can assign a onClick handler to a button in two ways.

Using the android:onClick XML attribute where you just use the name of a public method with the signaturevoid name(View v) or by using the setOnClickListener method where you pass an object that implement the OnClickListener interface. The latter often requires an anonymous class which personally I don’t like (personal taste) or defining an internal class that implements the OnClickListener.

By using the XML attribute you just need to define a method instead of a class so I was
wondering if the same can be done via code and not in the XML layout.

17 Answers
17

Leave a Comment