Advantage and disadvantage between onClick and setOnClickListener

7

I have a certain situation where we see that it is possible to create in the XML file a property called onClick :

onClick:

XML:

<Button
android:layout_height="@dimen/edittext_min_height"
android:layout_alignParentLeft="true"
android:minWidth="120sp"
android:onClick="btnSend"
android:clickable="true"/>

Class:

public void btnSend(View view){
    //faça algo
}

On the other hand we can also instantiate within the class the setOnClickListener() :

setOnClickListener:

XML

<Button
    android:id="@+id/btnSend"
    android:layout_height="@dimen/edittext_min_height"
    android:layout_alignParentLeft="true"
    android:minWidth="120sp"/>

Class:

Button btnSend = (Button) findViewById(R.id.btnSend);
btnSend .setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
      //faça algo
   }
});

What would be the advantages and disadvantages between using onClick and setOnClickListener ?

    
asked by anonymous 26.10.2016 / 14:09

4 answers

8

I only find an advantage in each of them, which, in turn, represent a disadvantage compared to the other.

setOnClickListener() .

  • Advantage.
    Allows reuse of existing code.
    If you implement the interface in a separate class it can be reused in other similar situations.
  • Disadvantage.
    The implementation, being done in an anonymous class, makes the code more difficult to read and maintain. Of course it is debatable and some say otherwise. In my understanding it is code that "pops out of the blue", in the middle of the one I'm interested in reading / analyzing.

onClick in XML

  • Advantage.
    Easier to read code. The code is in a declared method for this purpose.
    You can avoid the need to have an attribute and use findViewById()
  • Disadvantage.
    The code can not be reused.
    Only works when view is used in an Activity.

For simple implementations and no need to reuse code, use onClick in XML with its method in java. For more complex implementations and / or code reuse, create a separate class and use setOnClickListener() .

    
26.10.2016 / 15:32
3

I believe there is only one difference or a different mode of application. The methods are similar, so do you indicate your action by clicking the button in XML how to handle this action by Listener.

It turns out that by using android:onClick the setOnClickListener method is implementing the same way, only you do not see it. They do the same thing. And yes, you can handle several different actions using the attribute in XML.

See an example:

  

XML

<Button android:id="@+id/b_1"
    android:onClick="actionHandle"/>

<Button android:id="@+id/b_2"
    android:onClick="actionHandle"/>
  

Java

public void actionHandle(View v){
    switch(v.getId()){
        case R.id.b_1:
            // act!
            break;

        case R.id.b_2:
            // act!
            break;
    }
}

See that you can use the actionHandle method or any method you set to handle the click that occurs in your botões . If you're going to use setOnClickListener , it would look something like this:

  

setOnClickListener

public class A extends Activity {

    @Override
    protected void onCreate(Bundle b){
        super.onCreate(b);
        // setContent

        (findViewById(R.id.b_1)).setOnClickListener(actionHandle);
        (findViewById(R.id.b_2)).setOnClickListener(actionHandle);
    }

    View.OnClickListener actionHandle= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.b_1:

                    break;

                case R.id.b_2:

                    break;
            }
        }
    };
}

In my opinion , using the attribute in XML is more malleable and organized, and of course, if you know how to use the attribute without creating multiple methods for different buttons, is the best choice no matter the size of the application.

    
15.01.2017 / 04:33
0

There is no more correct way.

But the second option setOnClickListener in activity is recommended as good practice. By having a faster view of the function that the method has. For maintenance, or even during development, using onClick in XML may end up generating a more confusing code.

    
26.10.2016 / 14:35
0

I had the same question and I went into the subject and then this answer I ended up convincing myself not to use onClick . Basically: Android button implementations indicate that the button will be clickable, however, if any manufacturer points it to false, you will only feel the problem through the market. You can force the button to be clickable, but forget it, a hug!

EDIT: As I identified in the answer, the problem is to set the Click event on a non-button object.

    
01.11.2017 / 02:28