Best practice android:onClick XML attribute or setOnClickListener? [duplicate]

I am not using the XML onClick attribute because that means the Activity that is inflating the XML must implement the onClick value method. But if you do some refactoring and you change this method, then you’ll get runtime exceptions if the changes are not correlated to XML. Or if you want to use some include or merge.

To add more: if you use fragments you have to delegate the click event to the fragment that defined onClick XML attribute.

It’s less code indeed, but in order to maintain/refactor such code it makes things difficult and open to errors. So don’t use it in production code.

Leave a Comment