Its possible to defined listeners both programmatically or in the XML definition of the View. What is best practice? How can I decide which of the two should be used?
Its possible to defined listeners both programmatically or in the XML definition of the
Share
I’m not sure there is a best practice. It probably comes down style differences, although there are times when using a real
OnClickListeneris superior to defining theonClickmethod in the XML.For example if you want to accumulate any state that is to be associated with the
OnClickListenerand you want yourOnClickListenerto be independent from yourActivitythen it would make sense to define a separate class that implements theOnClickListenerinterface. You simply cannot do this using the XML.However if you require simple actions be taken when a View is clicked than a simple XML definition will probably be sufficient.
The take away message isn’t that one is obviously better than the other but rather you should understand the strengths and weaknesses of both and use whichever better solves the problem at hand.