View.OnClickListener question

Discussion in 'App Development' started by deanstreet, Aug 13, 2021.

  1. I am wondering what's the difference between passing View.OnClickListener() an OnClickListener instance vs an View.OnClickListener **subclass** instance?

    Button myBtn = (Button) findViewById(;
    //OnClickListener instance
    private View.OnClickListener myBtnListener = new View.OnClickListener() {
            public void onClick(View v) {
                myBtn.setText("You Clicked on My Belly Button");
    //OnClickListener subclass instance
    myBtn.setOnClickListener(new BtnListener());
    private class BtnListener implements View.OnClickListener {
            public void onClick(View v) {
                v.setText("You Clicked on My Belly Button");
  2. Girija


    Inheritance. You have the flexibility to add your own methods which aren't in the super class.
  3. I thought it was rather clear that I wasn't asking for what is a subclass that every taxi driver knows, but specifically about setOnClickListener, which only overrides onClick(View v).

    I think in my example they are the same, but usually it is the Activity class, class MyActivity extends Activity implements View.OnClickListener, that subclasses the Lisntener Interface such that we can do btn.setOnClickListener(this);.