How Exactly Does the Android:Onclick Xml Attribute Differ from Setonclicklistener

How exactly does the android:onClick XML attribute differ from setOnClickListener?

No, that is not possible via code. Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute.

Those two code snippets are equal, just implemented in two different ways.

Code Implementation

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myFancyMethod(v);
}
});

// some more code

public void myFancyMethod(View v) {
// does something very interesting
}

Above is a code implementation of an OnClickListener. And this is the XML implementation.

XML Implementation

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myFancyMethod" />
<!-- even more layout elements -->

In the background, Android does nothing else than the Java code, calling your method on a click event.

Note that with the XML above, Android will look for the onClick method myFancyMethod() only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.

Another important thing I noticed. You mentioned you don't prefer anonymous methods. You meant to say you don't like anonymous classes.

Android onClick in XML vs. OnClickListener

Difference Between OnClickListener vs OnClick:

  • OnClickListener is the interface you need to implement and can be set
    to a view in java code.
  • OnClickListener is what waits for someone
    to actually click, onclick determines what happens when someone
    clicks.
  • Lately android added a xml attribute to views called android:onclick,
    that can be used to handle clicks directly in the view's activity
    without need to implement any interface.
  • You could easily swap one listener implementation with another if you need to.
  • An OnClickListener enable you to separate the action/behavior of the click event from the View that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the code
  • Since OnClickListener is an interface, the class that implements it has flexibilities in determining the instance variables and methods that it needs in order to handle the event. Again, this is not a big deal in simple cases, but for complex cases, we don't want to necessary mix up the variables/methods that related to event handling with the code of the View that triggers the event.
  • The onClick with function binding in XML Layout is a binding between onClick and the function that it will call. The function have to have one argument (the View) in order for onClick to function.

Both function the same way, just that one gets set through java code and the other through xml code.

setOnClickListener Code Implementation:

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myFancyMethod(v);
}
});

// some more code

public void myFancyMethod(View v) {
// does something very interesting
}

XML Implementation:

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myFancyMethod" />
<!-- even more layout elements -->

Performance:

Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.

Limitation:

android:onClick is for API level 4 onwards, so if you're targeting < 1.6, then you can't use it.

What's the difference between setting an onClickListener and making an xml android:onclick attribute related method?

OnClickListener is the interface you need to implement and can be set to a view in java code.

Lately android added a xml attribute to views called android:onclick, that can be used to handle clicks directly in the view's activity without need to implement any interface.

Both function the same way, just that one gets set through java code and the other through xml code.

Button setOnClickListener() vs android:onclick

The onClick with function binded in XML is a bind between onClick and the function that it calls. The function will have only one argument in order for onClick to function.

An OnClickListener is an interface that any class could implement. Since it is an interface that any class could implement, this is more flexible.

You could easily swap one listener implementation with another if you need to.

An OnClickListener enables you to separate the action/behavior of the click event from the View that triggers the event. While for simple cases this is not such a big deal, for complex event handling, this could mean better readability and maintainability of the code

In other words -

XML onClick is good for one fixed implementation in your Java code.
OnClickListener is better for more complex codes and multiple buttons. But as for the basic function - they both do the exact same thing.

Difference between OnClick() event and OnClickListener?

I'm not sure the question is clear. View.OnClickListener is an interface, which defines the onClick(View) method. If you have a class which intends to listen for clicks, you should both implement the interface (if not already extending a class that does), and implement this method too. You have to use both; they're not somehow alternatives.

How does XML onClick work without having an event listener?

Basically when the attributes (like layout_width, onClick and so on) are parsed during creation of the View, an onClickListener is created for this View if this attribute has been set in XML.
You can look this up for example here since it's open source.

Keep in mind that I was looking at the View class since Button extends TextView and TextView extends View.

To explain it a bit further: When you create a View via XML, all the attributes will be parsed. Then the properties of the View are set according to those attributes. You can also do this yourself when defining a custom View.

Answering your question simply: Yes an onClickListener is created "hidden" behind the XML during View creation

Which One Is Better Performance Wise: setOnClickListener VS android:onclick=onClick

Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.



Related Topics



Leave a reply



Submit