14

sorry if this question is stupid, but I can't wrap my head around Java syntax..I learnt C/C++
I know View is a class which is good..but I don't understand if View.OnClickListener() is a method.
I doubt it unless it returns an object?
I think View is a class which has a static OnClickListener member object..again that doesn't make sense to me..
Can some explain what is happening with this line of code?

button1 = (Button) findByView(R.id.button1)  ;
    button1.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {

        }
    }

So what is happening with this code?

Button1 is a reference to the button1 object in the xml file.

button1 object has a member object setOnClickListener which I from its name I assume initializes an event to the button or something. But it receives View.OnClicListener() object.

I am confused by that..onClick receives a View object so onClickListener is not an object returns a View object?? I don't get it at all.

Can someone explain what happens in that line View.onClickListener() is it another way of saying new this?

MByD
  • 135,866
  • 28
  • 264
  • 277
Lews Therin
  • 10,907
  • 4
  • 48
  • 72

3 Answers3

25

View.OnClickListener is an interface, you don't call it, but creates a new instance of it (new View.OnClickListener() is a call to the constructor)

The instance you create is of anonymous class that implements View.OnClickListener, in the brackets right under new View.OnClickListener()

Any class that implements View.OnClickListener must implement the methods declared in it (e.g. onClick)

setOnClickListener just saves the reference to the View.OnClickListener instance you supplied, and when someone clicks the button, the onClick method of the listener you set is getting called.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • 14
    small addition: don't be confused by the `.` in `View.OnClickListener`. It hasn't do to anything with a method call, the `OnClickListener` interface is just defined inside the class `View` - therefore you have to access the Interface via the class: `View.OnClickListener`. (You could also `import android.view.View.OnClickListener` and access the interface via `OnClickListener` but I don't know if that is against some andriod coding conventions) – pmnt Aug 04 '11 at 19:18
  • 1
    @MByD , as we know this keyword will point to current class , lets say- Class A extends Activity implements View.Onclicklistner { // for any view view.setOnclickListner(this) ; // so in this case this will point to Activitty class , how it will take listner ? } – Vaibhav Jain Jan 16 '17 at 07:07
2

OnClickListener is an interface. An interface provides a set of Methods other classes can implement. http://download.oracle.com/javase/tutorial/java/concepts/interface.html

You could have another class (Like and adapter), that extends OnClickListener, then your Adapter class could add the method "OnClick(View v)", and it would also be able to handle Click events. Or you could use the code you posted, where you just create an anonymous class, that implements OnClickListener.

-Woody

Woody
  • 363
  • 2
  • 14
  • Thanks, I read the link. So an interface is like a separate class that holds a list of functions that can be implemented by other classes that extends it. So the View class extends the OnClickListener interface or is it a method in an interface? – Lews Therin Aug 04 '11 at 19:05
  • 1
    Think of it like this. View has a local variable expecting a class that implements "OnClickListener" interface. When you call the View.set... method, it fills that variable with whatever you supplied it, which could be a class that implements "OnClickListener", or an anonymous class (Which is what you have in your original post) Then, whenever the View handles a click, it can call the OnClick function in the class you supplied. Don't forget to mark someones reply as the Answer to your question (Click the green check next to it) – Woody Aug 04 '11 at 19:16
2

Android code is geared for event based responses. The block of code is as follows:

Find a button that you've added to the active layout, and assign it to a local variable:

button1 = (Button) findByView(R.id.button1);

Set the on click listener for the button. This is a class that will be invoked when the button registers an event. The class is constructed here, it is anonymous as you don't assign it to a variable, but android will keep track of the reference.

Button events are always due to being pressed, so when the button registers that it has been pressed, it will inform the onClickListener class that the even occurred, and pass itself in as the view. The onClickListener is constructed as:

new View.OnClickListener() 
{
    public void onClick(View v)
    {

    } 
}

That onClick method is used by the listener to handle the event (in this case, a button press). So, you would put the code you would like executed in that method.

To answer your question directly, the onClickListere is an anonymous class that defines the onClick method, which will handle button events.

Noah
  • 1,966
  • 1
  • 14
  • 29