title: Best way to implement View.OnClickListener in Android
body: <p>Suppose we have an Activity with a lot of views on which <code>OnClickListener</code> is to be registered.</p>
  <p>The most common way to implement this is to let the Activity-Subclass implement the OnClickListener, something like this:</p>
  <pre><code>public class ActivityMain extends Activity implements View.OnClickListener
  {   
      @Override
      public void onClick(View view)
      {
          switch (view.getId())
          {
              //handle multiple view click events
          }
      }
  }
  </code></pre>
  <p>The way I like to implement it is to create a private class inside the Activity-Subclass and let that inner class implement the OnClickListener:</p>
  <pre><code>public class ActivityMain extends Activity implements View.OnClickListener
  {
      private class ClickListener implements View.OnClickListener
      {   
          @Override
          public void onClick(View view)
          {
              switch (view.getId())
              {
                  //handle multiple view click events
              }
          }
      }
  }
  </code></pre>
  <p>This way the code seems more organized and easy to maintain.</p>
  <p>Moreover, talking about &quot;Is-a&quot;, &quot;Has-a&quot; relationships, the latter seems to be a good practice because now the Activity-Subclass would have a &quot;Has-a&quot; relationship with the ClickListener.
  While in the former method we would be saying that Our Activity-Subclass &quot;Is-a&quot; ClickListener, which ain't completely true.</p>
  <p>Note that, I am not concerned with the memory overhead the latter would cause.</p>
  <p>Also, adding onClick tag in xml is completely out of question.</p>
  <p>So, what really is the best way to implement a ClickListener?</p>
  <p>Please don't suggest any libraries like RoboGuice or ButterKnife etc.</p>
  <p><strong>UPDATE:</strong></p>
  <p>I would like to share the approach I finally adopted.</p>
  <p>I directly implement the listener in Activity/Fragment.</p>
  <p>As far as OOP design is concerned. The <strong>&quot;HAS-A&quot;</strong> approach doesn't offers any practical benefits and even takes up more memory. Considering the amount of nested classes (and the memory overhead) we will be creating for every similar listener we implement, this approach should clearly be avoided.</p>