Home > DeveloperSection > Forums > Which is better way performance wise , andriod:onClick or set onClickListener for android objects
Jayne Spooner
Jayne Spooner

Total Post:21

Posted on    July-21-2015 11:39 PM

 Android Android  Android Controls 

 1 Reply(s)
 619  View(s)
Rate this:
Android docs mention two ways to set an onclick event on a button :
1.Using the android:onClick XML attribute where you just use the name of a public method with the signaturevoid name(View v) 
2. By using the setOnClickListener method where you pass an object that implement the OnClickListener interface. 

Which one is  correct ways ? Which is better techniques performance wise?

Please Expalin why

Mayank Tripathi
Mayank Tripathi

Total Post:397

Posted on    July-22-2015 8:09 AM

I think performance wise there is not much difference in both the approaches,The xml approach , android: onClick is provided to maintain clean coding in our activity.
But if  we define the code implementation in xml's on click method   we need to make the method always as public, i.e. we are exposing our implementation unnecessarily  which is not a good pratice, so my personal fav is the other way,  i prefer setOnclickListener way over the xml approch. This way we can make the implementation private.

Don't want to miss updates? Please click the below button!

Follow MindStick