Will the performance be any better using onClick? If I use onClick I do not have to set an android:id (also avoid's a new View.OnClickListener), does this improve performance at all? Or is the same effect of a findViewById occuring behind the scenes?
This page gives both methods as an option but little guidance on any benifit.
http://developer.android.com/reference/android/widget/Button.html
Here's a blog post where they deem onClick as "easier" and an "improvement" for post 1.6 applications;
http://android-developers.blogspot.com/2009/10/ui-framework-changes-in-android-16.html
This new feature reduces both the amount of Java and XML you have to write, leaving you more time to concentrate on your application.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
Use OnTouchListener when you want to receive events from someone's finger on the screen. Use OnClickListener when you want to detect clicks.
To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
OnClickListener, you can defined your click of each button in method public void onClick(View v) . Keyword this refer to the method onclick. It is good to use this way when there are a lot of button in your class file.
I believe that the inclusion of android:onClick
has been a very bad idea.
android:onClick
approach you can even forget that you have a button in your layout or which is the method that is handling its onClick
event.I would suggest you stick to defining your OnClickListener
s programatically and keep a strict separation of concerns, as Corey Sunwold pointed out in his comment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With