Android - setOnClickListener vs OnClickListener vs View.OnClickListener
The logic is simple. setOnClickListener
belongs to step 2.
- You create the button
- You create an instance of
OnClickListener
* like it's done in that example and override theonClick
-method. - You assign that
OnClickListener
to that button usingbtn.setOnClickListener(myOnClickListener);
in your fragments/activitiesonCreate
-method. - When the user clicks the button, the
onClick
function of the assignedOnClickListener
is called.
*If you import android.view.View;
you use View.OnClickListener
. If you import android.view.View.*;
or import android.view.View.OnClickListener;
you use OnClickListener
as far as I get it.
Another way is to let you activity/fragment inherit from OnClickListener
. This way you assign your fragment/activity as the listener for your button and implement onClick
as a member-function.
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.
new OnClickListener() vs new View.OnClickListener()
Eclipse automatically imports View.OnClickListener
so you don't have to use the View.
prefix.
If you import android.view.View.OnClickListener;
you can just use OnClickListener
but
if you import android.view.View;
you have to use View.OnClickListener
Check how your imports list changes and it'll make more sense.
Difference between new View.OnClickListener and new OnClickListener
If you're imported View.OnClickListener
, both will refer to the same class and will work identically.
View.OnClickListener
is just often used to distinguish from DialogInterface.OnClickListener
.
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
.
Difference between specifying the view.onclicklistener and just having onclicklistener
There are 2 of setOnClickListener
one for the View
class and one refer to DialogInterface
Class.
So to in order to manipulate the View like a Button
or ImageView
and add an action to it, you need to use View.OnClickListener
while dealing with Dialog buttons you should use DialogIneterface.onClickListener
both have different arguments.
Usually by adding onClickListener
, the View Class will be imported by default or it will make you choose between both classes. so you don't need to add View.onClickListener
. However, if the class DialogInterface
have been imported already and you want to use the View onClickListener then you have to write View.onClickListener
to differentiate both classes' onClickListener
.
Hope it is clear now and this is what you are looking for.
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.
Related Topics
How to Create a Resizable Rectangle with User Touch Events on Android
How to Put Media Controller Button on Notification Bar
Resizing Layouts Programmatically (As Animation)
Android - How to Make Slide Menu Like Facebook, Spotify and Google +
Reading SQLite File from Asset Folder
Android Facebook 4.0 Sdk How to Get Email, Date of Birth and Gender of User
Convert Arraylist<Mycustomclass> to JSONarray
Nullpointerexception When Trying to Access Views in a Kotlin Fragment
Using Seekbar to Control Volume in Android
Appcompatv7 - V21 Navigation Drawer Not Showing Hamburger Icon
How to Add Footer to Navigationview - Android Support Design Library
Preventing Status Bar Expansion
How to Get Access Token After User Is Signed in from Gmail in Android
Android Java.Lang.Noclassdeffounderror
Parse JSON Array Without Key in Android
Simulate Low Network Connectivity for Android