21

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.

RandomNickName42
  • 5,923
  • 1
  • 36
  • 35
  • 1
    I don't know whether or not there is any performance impact (and I doubt there is) but I'd be willing to bet its just a better idea to apply an OnClickListener to keep the activity easy to maintain, and keep a strict separation of concerns. – Corey Sunwold Mar 23 '11 at 02:42
  • After your last edit: it may be easier, but I don't think my onClick handlers are boilerplate code anyway. It always ends in a matter of personal taste... – mgv Mar 23 '11 at 04:32
  • I'm still stuck on understanding the performance hit of findviewbyid, I've seen some people say they can be expensive and such, so my thought was that reducing the calls onclick may help. Plus the reduction of id's being set (unless they are simply done behind the scenes when you do not set them specifically) may help speed that function when you do need it. I had taken over some code where it seemed that android:id was excessivly set, which leads me to believe in the long run may over burden findviewbyid... – RandomNickName42 Mar 23 '11 at 05:01
  • I think that if this was something important to take into account for performance it will be at least mentioned in the Designing for Performance best practices article on developers.android.com, but it is not there, so I guess there's nothing to be worried about. Moreover, not everything is performance. – mgv Mar 23 '11 at 12:31
  • see also http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments – Paul Verest Dec 25 '14 at 03:29

4 Answers4

35

I believe that the inclusion of android:onClick has been a very bad idea.

  1. You are coupling presentation with logic
  2. Unless you are using a plugin that supports it, you will have to remember to refactor the xml file if you decide to change your method name
  3. It's just not clear the relationship between a button in your xml and a method in your activity that reacts to the click events unless you explicitly see it defined in your Java file. With the 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 OnClickListeners programatically and keep a strict separation of concerns, as Corey Sunwold pointed out in his comment.

mgv
  • 8,384
  • 3
  • 43
  • 47
  • You are coupling presentation with logic. Nice statement. – Joaquin Iurchuk Aug 23 '15 at 18:43
  • @joaquin this is not nice. it's just confusing. don't have real meaning I think. go and read other threads. many of them says both methods are similar but depends on usage. – David Aug 29 '15 at 12:01
  • @David I meant that "the inclusion of the onClick method has been a very bad idea because of coupling presentation with logic". And that's the nice statement. – Joaquin Iurchuk Aug 29 '15 at 14:23
0

You might consider using an onTouchListener instead. I've found it to be faster, particularly if you want the action to occur on press as opposed to release.

bobeast
  • 9
  • 1
  • But the best practice for a button's action must be occur on release. Because we can show animation or selector there and user can cancel the click itself by move the finger out of the button when holding the touch – HendraWD May 02 '16 at 04:02
0

I am trying to think about how to test this.... But I believe there is no performance difference between the two, they are just different interfaces for the same thing.

Matthieu
  • 16,103
  • 10
  • 59
  • 86
  • I think it might depend on how setting the onclick in the XML compiles, so they might not be exactly the same. Either way I doubt there is any performance impact. – Corey Sunwold Mar 23 '11 at 02:43
0

I use android:onClick when I just need my Button to be clickable.

If I need to do something else to the Button, for example enable/disable it, I would use setOnClickListener() as mgv said.

Also remember that android:onClick doesn't work in Android 1.5.

Macarse
  • 91,829
  • 44
  • 175
  • 230