0

I am learning how to develop apps in Android Studio and have just begun. I learnt 2 methods of handling button click events. One is to implement OnClickListener while the other is to go to XML and just use Android:OnClick. The second method is far easier for me. Is there any advantage of the first method or is just knowing the second method enough?

Thanks Nigpig

Kalyan Nadimpalli
  • 319
  • 1
  • 2
  • 10

3 Answers3

6

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.

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
Sangeeta
  • 961
  • 2
  • 13
  • 34
1

onClick and OnClickListeners have the same functionality when it comes to simple programs. But when it comes to complex programs, onClick cannot provide the functionality of OnClickListeners.

There are a couple reasons why you might want to programmatically set an OnClickListener. The first is if you ever want to change the behavior of your button while your app is running. You can point your button at another method entirely, or just disable the button by setting an OnClickListener that doesn't do anything.

When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button's behavior from somewhere other than its host activity. This is a major part of Fragments, which are basically mini activities, allowing you to build reusable collections of views with their own lifecycle, which can then be assembled into activities. Fragments always need to use OnClickListeners to control their buttons, since they're not Activities, and won't be searched for listeners defined in onClick.

Nirmal Dalmia
  • 317
  • 3
  • 12
0

Kindly search for the answer first before asking. Here is enough explanation what you are searching for. Have a look Here

Community
  • 1
  • 1
Shree Krishna
  • 8,474
  • 6
  • 40
  • 68