Home » Android » java – setOnClickListener(new OnClickListener(){})

java – setOnClickListener(new OnClickListener(){})

Posted by: admin June 15, 2020 Leave a comment

Questions:
package android.example;

import android.app.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Android_eg1 extends Activity {
    Button bt; 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.click);
        bt.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
            // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "You made a mess", Toast.LENGTH_LONG).show();
            }

        });
      } //onCreate()

}//class

I am new to java and I know basics of core java.
This is the sample program i tried to know about the event handling through a button class.
I could not understand this part :

"bt.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
        // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "You made a mess", Toast.LENGTH_LONG).show();
        }

    });"

Inside the setOnClickListener(parameter) why are they giving a function definition?(public void onClick(View v) })

Is this acceptable?

How to&Answers:

In java, this is a Generated Class implementing the interface. You can generate a class on the fly inside the function argument or you can create an external class and passing a instance of this class or implements the Interface needed by your listener by your activty and passing your activity as listener.

Answer:

Button click is an event handle. You can clearly understand the difference, if you implement the OnClickListner rather than using in your example code. When you implement the OnClickListner your IDE shows the option to use unimplemented methods related to OnClickListner interface. There you can see public void onClick method. If I summarize the scenario;

  • You have a button
  • You should have an interface that listens to the button events –
    OnClickListner
  • Set your button event to listen to the user events –
    button.setOnClickListner(this)
  • What do you want to do after the event comes inside the onClick method

This full example code may give you a clear understanding on that.

Answer:

setOnClickListener takes an OnClickListener object as the parameter. Basically it’s creating an anonymous subclass OnClickListener in the parameter.

It’s like the same in java when you can create a new thread with an anonymous subclass, ex:

Thread t = new Thread(new Runnable()
    {     
        public void run(){...}
    });

Answer:

OnclickListener is an interface(Listener) and setOnClickListener() is a method that needs a object of type onClickListener. We can create class anonymously. ie, done by calling a class of no name (ANONYMOUS CLASS DECLARATION) which implements OnClickListener and thus it will have to implements void onClick method. Thus we will have the object of type interface and this is used for shorthand programming. For further clearance about anonymous class please visit http://www.techartifact.com/blogs/2009/08/anonymous-classes-in-java.html

Answer:

setOnClickListener function with parameter of type OnClickListener()

OnClickListener() is an interface:(analogy to c++ abstract base class)

the code:

    bt.setOnClickListener(new OnClickListener()

    /* this is the definition of anonymous class which implements OnClickListener() */    
  /*  equivalent to : 
class OnClickListener_implementation implements OnClickListener */  
    {
            public void onClick(View v)
                         {
                            //your code here
                           }

    );

This means: Construct an object of a class that implements the OnClickListener interface by declaring the onClick as specified