71

Possible Duplicate:
Difference between OnClick() event and OnClickListener?

I'm semi-new to Android development and when I first started I tried to avoid using the xml layout by any means necessary so some of my earlier projects involve buttons that explicitly create an OnClickListener and implement it as an anonymous inner class. Such as -

final Button button = new Button(this);
button.setText("Click to change second line of text");

OnClickListener buttonListener = new View.OnClickListener() {
    boolean clicked = false;
    int numClicks = 0;

    @Override
    public void onClick(View v) {
        if(numClicks > 5) {
            button.setText("STOP IT");
        }
        numClicks++;
        if(clicked == false){
            clicked = true;
            tv2.setText("Text Changed on Button Click");    
        }
        else
        {
            clicked = false;
            tv2.setText("Click again");
        }       
    }
};
button.setOnClickListener(buttonListener);

But as I got more familiar with android, I began to understand the value of the xml layouts and implemented buttons like this

    <Button
    android:id="@+id/button1"
    android:layout_height = "wrap_content"
    android:layout_width ="wrap_content"
    android:text = "lets do this"
    android:onClick = "DoIt"
    />

In the layout xml, where DoIt was defined in the java.

My question is, are these 2 methods functionally the same thing? Is there an OnClickListener being defined by the compiler somewhere behind the scenes? Are there any features you trade off by using one way or the other?

rohegde7
  • 633
  • 9
  • 15
SmashCode
  • 4,227
  • 8
  • 38
  • 56
  • 1
    [Here is a useful discussion](http://stackoverflow.com/a/7453469/1154026) I think will help with your question. – Vic Vuci Jan 23 '12 at 19:18
  • You can set click listener by three ways, 1. setting individual click listener for each element. 2. implementing OnClickListener in activity. 3. creating a separate function for handling button click and add this function in xml. you can find these example here http://wiki.workassis.com/android-three-ways-to-set-click-listener/ – Bikesh M Jul 22 '16 at 06:26

3 Answers3

173

These are exactly the same. android:onClick was added in API level 4 to make it easier, more Javascript-web-like, and drive everything from the XML. What it does internally is add an OnClickListener on the Button, which calls your DoIt method.

Here is what using a android:onClick="DoIt" does internally:

Button button= (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DoIt(v);
    }
});

The only thing you trade off by using android:onClick, as usual with XML configuration, is that it becomes a bit more difficult to add dynamic content (programatically, you could decide to add one listener or another depending on your variables). But this is easily defeated by adding your test within the DoIt method.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
  • Excellent answer. Thanks for explaining what Android does behind the scenes when setting the callback via XML. – Joshua Pinter Mar 14 '14 at 17:00
  • 6
    It's worth noting that the `android:onClick` won't work with a method defined in a fragment. The best solution imho is to just attach the listeners within the fragment. For more info: http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments – adamdport Feb 24 '15 at 22:46
  • Thanks for the brief explanation. – Sandeep Yohans Dec 08 '17 at 05:22
  • Which class is instantiated when we say new View.OnClickListener() and whose instance is passed in as the parameter for the setOnClickListener. – Tom Shaw May 10 '21 at 10:16
4

using XML, you need to set the onclick listener yourself. First have your class implements OnClickListener then add the variable Button button1; then add this to your onCreate()

button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);

when you implement OnClickListener you need to add the inherited method onClick() where you will handle your clicks

Bill Gary
  • 2,987
  • 2
  • 15
  • 19
2

Even though you define android:onClick = "DoIt" in XML, you need to make sure your activity (or view context) has public method defined with exact same name and View as parameter. Android wires your definitions with this implementation in activity. At the end, implementation will have same code which you wrote in anonymous inner class. So, in simple words instead of having inner class and listener attachement in activity, you will simply have a public method with implementation code.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 1
    I do have it implemented in my java, I just didn't post it because the method wasn't relevant to the question. – SmashCode Jan 23 '12 at 19:36