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.
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 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.
OnclickListener vs OnClick
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.
Both function the same way, just that one gets set through java code and the other through xml code.
setOnClickListener Code Implementation:
Button button = (Button) findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
yourMethod(v);
}
});
public void yourMethod(View v) {
// does something very interesting
}
XML Implementation:
// method to be written in the class
public void yourMethod(View v) {
// does something very interesting
}
//XML file
<?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="yourMethod" />
Both are the same in performance. Xml is pre-parsed into binary code while compiling. so there is no over-head in Xml.
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.
What are the differences in using onClick in the xml and button.setOnClickListener in the activity?
One is by defining android:onClick="someFunction" and another is by having a listener in the activity.
Not exactly. Your choices are:
Use
android:onClick
, pointing to a method in the activity that hosts this widget, using the native implementation of this (what we have had since Android 1.6)Call
setOnClickListener()
on theView
, passing in an implementation of anOnClickListener
Use the data binding framework to tie
android:onClick
to an arbitrary method or an arbitrary lambda expression
Are they the same and if I can use them interchangeably.
They are the same, insofar as both define behaviors to be invoked if the user clicks the widget.
Are there things I can do with one and not the other? Any limitations?
Option #1 is limited to methods implemented on the hosting activity. Option #2 and Option #3 are not. With those, you can implement the logic to be invoked on a click event as part of some UI controller or presenter, such as a fragment.
Is it just up to opinion which is better practice?
Well, pretty much everything is up to opinion.
IMHO, Option #1 is fine for trivial apps, such as book examples. Option #3 is new but powerful, and I anticipate that this will be the long-term solution.
onClickListener vs onClick()
onClick
in xml
just calls through to setOnClickListener
when the View is created. When you then call setOnClickListener
in code, it overrides the existing OnClickListener
that was set in xml
.
Related Topics
Android - How to Pass Hashmap<String,String> Between Activities
Android SQLite Issue - Table ... Has No Column Named
How to Build an Android Library with Android Studio and Gradle
Android: Share Plain Text Using Intent (To All Messaging Apps)
Cursor Adapter and SQLite Example
How to Shrink the Drawable on a Button
How to Debug Android Application Line by Line Using Eclipse
Android Runtimeexception: Unable to Instantiate the Service
Getlastknownlocation Always Return Null After I Re-Install the APK File via Eclipse
Programmatic Views How to Set Unique Id'S
Startactivity() from Broadcastreceiver
Android: Http Communication Should Use "Accept-Encoding: Gzip"
How to Make a Phone Call Programmatically
Error:(26, 0) Gradle Dsl Method Not Found: 'Runproguard()'
Move Markers in Google Map V2 Android