WHICH IS BETTER WAY PERFORMANCE WISE , ANDRIOD:ONCLICK OR SET ONCLICKLISTENER FOR ANDROID OBJECTS

Jayne Spooner

Total Post:21

Points:147
Posted by  Jayne Spooner
 684  View(s)
Ratings:
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
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Which is better way performance wise , andriod:onClick or set onClickListener for android objects

    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.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!