-3

I'm watching/reading some tutorials these days about Android app development. And I've learnt two different ways of assigning a button's onclick method. One is to declare it in XML:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />

The other is by finding the id the button:

Button myBtn = (Button) findViewById(R.id.button1);
myBtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        //do something...           
    }
});

My question is: what's the difference (advantage and disadvantage) of each method, and if the efficiency is different, one is faster than other so it's usually preferred?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Arch1tect
  • 4,128
  • 10
  • 48
  • 69
  • 2
    one more is there, to implement interface – Pankaj Kumar May 15 '13 at 06:33
  • [Here](http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) you have an answer. – mmBs May 15 '13 at 06:35
  • @mmBs oh, thanks, so they are almost totally same. – Arch1tect May 15 '13 at 06:40
  • @Arch1tect What performance issue are you facing in using one of them. There are three methods to apply onClickListener. Its not a long running process so you are facing any issue related to performance I think so. Can you please give any proof for difference in performance in them. – Nikhil Agrawal May 15 '13 at 06:43
  • @Er.NikhilAgrawal He never said he had a performance issue, he asked if there was a performance difference. – eski May 15 '13 at 06:44
  • @eskimoapps.com Ya I know I am just asking as he is asking question about performance. This is not a discussion forum if you are coming up something please make some research for that problem don't directly ask what is the differnece. First of all read some blogs article on google then ask So the community can know you are also serious about your question. See an example question http://stackoverflow.com/questions/15683952/pop-up-window-over-android-native-incoming-call-screen-like-true-caller-android – Nikhil Agrawal May 15 '13 at 06:51
  • @PankajKumar he is right absolutely +1 for you. – Nikhil Agrawal May 15 '13 at 06:53

2 Answers2

0

The efficiency difference is not going to matter because it would be so small, but my guess is that the XML method is slightly faster since it avoids object creation.

However, this assumes the overhead from whatever linking is done through the XML is smaller than the object creation overhead.

My advice: don't worry about small efficiency advantages in cases where something is executed very few times.

eski
  • 7,917
  • 1
  • 23
  • 34
-1

Usually it's easier to just put the button in the layout and then code the method in the activity class. You can also use button.setOnTouchListener and detect if the button has been pressed down or unpressed (to implement sounds change the buttons background when it is pressed).

As for the 1st method you described, I only used it when I had a listview and each item in the list was made up of a textview and a button. So I needed each of those many buttons to call a single method. I hope that answers your question.

Sebek
  • 642
  • 8
  • 22