title: Android - setOnClickListener vs OnClickListener vs View.OnClickListener
body: <p>My understanding is that when I'm creating a button object that listens for a click, I have to:</p>
  
  <ol>
  <li>Create the button object</li>
  <li>Use <code>OnClickListner</code> to make it listen to the user's click</li>
  <li>Use <code>onClick</code> to execute actions after the user clicks the button</li>
  </ol>
  
  <p>Now, </p>
  
  <ul>
  <li>Where does <code>setOnClickListener</code> fit into the above logic? </li>
  <li>Which one actually listens to the button click? </li>
  <li><code>setOnClickListener</code>? </li>
  <li><code>OnClickListener</code>? </li>
  <li><code>View.OnClickListener</code>? </li>
  <li>What are the differences between those three?</li>
  </ul>
  
  <p>Here is what I found in Android Dev:</p>
  
  <pre><code>//The example below shows how to register an on-click listener for a Button.
  
  // 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);
      ...
  }
  </code></pre>
  
  <p>You may also find it more convenient to implement <code>OnClickListener</code> as a part of your <code>Activity</code>. This will avoid the extra class load and object allocations. For example:</p>
  
  <pre><code>public class ExampleActivity extends Activity implements OnClickListener {
      protected void onCreate(Bundle savedValues) {
          ...
           Button button = (Button)findViewById(R.id.corky);
           button.setOnClickListener(this);
      }
  
      // Implement the OnClickListener callback
      public void onClick(View v) {
        // do something when the button is clicked
      }
  }
  </code></pre>