3

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?

vanguard69
  • 840
  • 1
  • 10
  • 18
  • possible duplicate of [What does "this" mean?](http://stackoverflow.com/questions/10921167/what-does-this-mean) – M D Feb 06 '15 at 08:53
  • 1
    Check out http://stackoverflow.com/a/17964932/1234007 – Aman Gautam Feb 06 '15 at 08:54
  • 2
    I don't get it. Why a downvote? They are different answers, might be that understanding them leads to the same conclusion. My question is - "how the method setOnClickListener(this) identifies that it should call onClick() method?" Please read it carefully! – vanguard69 Feb 06 '15 at 09:06
  • 2
    @Mooooooo Please do me a favor and go about downvoting all questions on this site. – vanguard69 Feb 06 '15 at 09:07
  • @Mooooooo I thought you downvoted my question, which now I'm assuming you didn't! Peace! :) – vanguard69 Feb 06 '15 at 11:11

5 Answers5

2

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.

nourikhalass
  • 360
  • 1
  • 2
  • 8
1

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?

Orkun
  • 6,998
  • 8
  • 56
  • 103
1

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.

VikasGoyal
  • 3,308
  • 1
  • 22
  • 42
0

Your code has

MainActivity implements OnClickListener

but actually it is:

MainActivity implements View.OnClickListener

Maybe that is what confuses you.

WindRider
  • 11,958
  • 6
  • 50
  • 57
0

"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.

Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20