-3

I know that keyword this refers to current instance of class. But When we implement View.OnClickListener in our class then on calling method textview.setOnClickListener(this), How does argument this(instance object of class) of setOnClickListener(this) call automatically onClick() method. Is there any code in view class which take object and call onClick method on this code or something else is going on?

I want to know what is going behind the scenes, how does android reaches from this keyword to onClick() method? That is what I want to ask?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Novice
  • 49
  • 11

3 Answers3

0

OnClickListener is an interface in class View.
If your activity implements this interface by setting:

public class MainActivity extends AppCompatActivity implements View.OnClickListener

then you can set a listener for a view like button:

myButton.setOnClickListener(this);

and override the onClick method implementing it like this:

@Override
public void onClick(View v) {
// your code here
}

so a simple explanation is: this means that your view will use your activity's overridden onClick method.

  • But I want to know what is going behind the scenes, how does android reaches from this keyword to onClick() method? That is what I want to ask? – Novice Jul 29 '18 at 08:48
  • This keyword directs to the overridden onClick. You must dig deeper about Java classes and interfaces. There are many tutorials to follow if you search. –  Jul 29 '18 at 08:53
  • Everywhere I have read about this , they say keyword this refers to current instance of class. Can you refer me to any article where it is properly explained? – Novice Jul 29 '18 at 08:59
  • Exactly, the keyword `this` means that **this activity** will handle the `onClick` method –  Jul 29 '18 at 09:01
0

Let's have an interface ElectricityBill

public interface ElectricityBill{
  public void pay(int amount);
}

now there are two ways you can pay an electricity bill either by going to nearby electricity board office

ElectricityBill bill = new ElectricityBill(){
  @Override
  public void pay(int amount){

  }
}
payBill(bill);

or by paying the bill online

    public class User implements ElectricityBill{
     .....
          @Override
          public void pay(int amount){

          }
        }
     .....
  payBill(this);

    }

in both the cases, the user has to pay XXX amount, similar is the case if you want to listen to input events you either have to pass the original View.OnClickListener object or implement it and pass this to make it work.

Edit:

when you pass this you tell the current class to handle the click events itself and has to override the onClick() to do so. And when you pass object you let the original class to handle the onClick by creating an anonymouse class and implementing onClick(). but when you pass this your current class get's the authority to listen to input events. It becomes on the type of OnClicklistener and gets the authority to listen onClick()

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • Question is how does this keyword directs to the overridden onClick method() ?Because Everywhere in every article they say this keyword refers to current instance of class. – Novice Jul 29 '18 at 09:02
  • What is payBill? –  Jul 29 '18 at 09:02
  • payBill() takes an instance of ElectricityBill which you either pass by creating an object or passing this. @Novice when you pass this you tell the current class to handle the click events itself and has to override the onClick() to do so. And when you pass object you let the original class to handle the onClick by creating an anonymouse class and implementing onClick(). but when you pass this your current class get's the authority to listen to input events. It becomes on the type of OnClicklistener and gets the authority to listen onClick(). – vikas kumar Jul 29 '18 at 09:11
0

To implement the View.setOnclickListener in your code you need to first implement the public static interface View.OnClickListener.

like this

    public class MyActivity extends Activity implements View.OnClickListener {
}

The above interface contains public void abstract method "onClick(View v)" which you override to put your logics This method is called when a view has been clicked.

for sake of simplicity i have created the code

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
    ...
}

here OnclickListener is an interface which have object mCorkyListener similarly you can use this keyword instead mCorkyListener ‘this’ represents the instance of the current class. You can access the properties and functions of the current class with ‘this’ keyword.

Parikshit Sharma
  • 307
  • 2
  • 14