How is using OnClickListener interface different via XML and Java code ?

How is using OnClickListener interface different via XML and Java code ?

Asked on January 9, 2019 in XML.
Add Comment


  • 6 Answer(s)

         The android:onClick was added in API level 4 to make it easy, more Javascript-web-like, and will drive whole thing from the XML. It will do internally is add an OnClickListener on the Button, which calls the DoIt method.

    Using a android:onClick=”DoIt” done by internally,

    Button button= (Button) findViewById(R.id.buttonId);
    button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          DoIt(v);
       }
    });
    

         The thing that we trade off by using android:onClick, as normal with XML configuration, is that becomes more difficult to add dynamic content. But it is easily defeated by adding the test within the DoIt method.

    Answered on January 9, 2019.
    Add Comment

         Set the onclick listener using XML. At first have the class implements OnClickListener then add the variable Button button1; then add this to our onCreate()

    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(this);
    

         when the user implement OnClickListener, we want to add the inherited method onClick() where we will handle the clicks

    Answered on January 9, 2019.
    Add Comment

         If the user termed on android:onClick = “DoIt” in XML, make sure that the activity or view context has public method termed with correct same name and View as parameter. The android wires our definitions with this implementation in activity. At the last, the implementation process will have same code which we wrote in anonymous inner class. So, in easy words instead of having inner class and listener attachement in activity, we will have a public method with implementation code.

    Answered on January 9, 2019.
    Add Comment
    106

    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.
    Answered on February 25, 2019.
    Add Comment
    utton 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
    }

     

    Answered on February 25, 2019.
    Add Comment

     they both are a way to add logic in response to an event, in this case a ‘click’ event.

    I would go for a separation between logic and presentation, just like we do in the HTML/JavaScript world: Leave the XML for presentation and add event listeners by means of code.

    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.