I'm new to android (and in general) mobile app development.

When it comes to setting an event listener to, say, a button, what is the difference between setting it in the xml:

android:onClick="blah"

and setting it in the .java file

btn.setOnClickListener(new View.onClickListener)

Any particular time when i should use the former or the latter? Or does it make no difference

Recommended Answers

The XML option does allow you setup listener that calls on custom method so in some cases it may look more straight forward in the way say on click of this button do some function execution.
However most developers use setOnClickListener where it is obvious from class that it does …

Jump to Post

All 2 Replies

The XML option does allow you setup listener that calls on custom method so in some cases it may look more straight forward in the way say on click of this button do some function execution.
However most developers use setOnClickListener where it is obvious from class that it does implements OnClickListener or you have it attached as inner class to button onClickListener setter.
There is not clean declaration in documentation when to use this or that, but as I said second option is more popular and common.

Thank you :)
-Jamil Baig

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.