vanguard69
vanguard69

Reputation: 840

How does setOnclickListner(this) work?

There are multiple ways to register callbacks when a Button is clicked. If I go by the following way:

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

@Override
public void onClick(View v) {
    Toast.makeText(this, "Hello onCLick", Toast.LENGTH_SHORT).show();
}
}

I don't understand how the method setOnClickListener(this) identifies that it should call onClick() method?

Upvotes: 3

Views: 173

Answers (5)

Orkun
Orkun

Reputation: 7268

I think I understand your confusion. When you read other SO answers or references like the View.OnClickListener, it feels like all the sentences are telling the same thing but nothing that really helps click.

What happens is, when a Button is clicked, it will notify all the objects that are listenning for it. You are subscribing your activity as a listener, to this event with the line

button.setOnClickListener(this);

So, on an event of a click, button knows that it should call the activity's onClick event.

I don't understand how the method setOnClickListener(this) identifies that it should call onClick() method?

(Therefore, it s the button that calls the listener.onClick() method, in case there's a confusion there.)

Also, @nourikhalass has a point, you should first make sure that interfaces make sense to you.

Is it any clearer?

Upvotes: 1

Juanjo Vega
Juanjo Vega

Reputation: 1440

"This" refers to current object.

To handle button clicks, an object must implement the "OnClickListener" interface and define what to do when clicks are received in "onClick" method. Then you can register that object as a listener for your button clicks.

In your case, your activity implements OnClickListener, and onClick shows a toast:

public class MainActivity extends Activity implements OnClickListener {
    ...

    @Override
    public void onClick(View v) {
        Toast.makeText(this, "Hello onCLick", Toast.LENGTH_SHORT).show();
    }

Therefore, your activity can handle button clicks, so you register it as a listener for your button:

button.setOnClickListener(this);

As "this" implements the required interface, is a valid listener.

Upvotes: 0

VikasGoyal
VikasGoyal

Reputation: 3376

if you are aware about oops 'this' refer the reference of current object of class. a good explanation is define here

In above case MainActivity reference is refer as this here.

public void setOnClickListener(OnClickListener l)

is setter method define in class Button which hold the reference on "OnClickListener".

when you set setOnClickListener(this) it define you are passing OnClickListener reference as your activity so to make your activity as type on OnClickListener you have to implement the interface OnClickListener in your activity class as it is showing in your code.

public class MainActivity extends Activity implements OnClickListener

Its a callback listener which have method "onClick" you have to override that method and when button is clicked that method is call by Button class so the event listener (which is you activity in current scenario) can listen to it.

Upvotes: 1

nourikhalass
nourikhalass

Reputation: 360

This refers to the activity. Because the Activity implements an OnClickListener calling button.setOnClickListener(this) gives the onClickListener that the Activity implements to setOnClickListener.

I recommend you look up info about implementing interfaces in Java if you want tot know more about this practise.

Upvotes: 2

WindRider
WindRider

Reputation: 12318

Your code has

MainActivity implements OnClickListener

but actually it is:

MainActivity implements View.OnClickListener

Maybe that is what confuses you.

Upvotes: 0

Related Questions