title: OnClick on inflated items
body: <p>In my <strong>activity.xml</strong> I have a <code>LinearLayout</code>. This <code>LinearLayout</code> contains an inflated layout consisting of four <code>ImageViews</code>. Implementation is something like this:</p>
  
  <pre><code>LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_template);
  View view = getLayoutInflater().inflate(R.layout.inflated_layout, linearLayout, false);
  linearLayout.addView(view);
  </code></pre>
  
  <p>I want to implement an action if the user clicks on the image. However, nothing happens when I click on the image. This is my implementation for onclick event:
  <code>v.getId()</code> gives me <code>-1</code> every time I click an image </p>
  
  <pre><code>view.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Log.v("V.getID", String.valueOf(v.getId()));
              Log.v("Image 1 id", String.valueOf(R.id.iv_t_1));
  
              switch (v.getId()){
  
                  case R.id.iv_t_1: //image 1 clicked
                      //action
                      break;
  
                  case R.id.iv_t_2: //image 2 clicked
                      //action
                      break;
  
                  case R.id.iv_t_3: //image 3 clicked
                      //action
                      break;
  
                  case R.id.iv_t_4: //image 4 clicked
                      //action
                      break;
  
                  default:
                      break;
              }
          }
      });
  </code></pre>
  
  <p><strong>EDIT:</strong> The reason i don't want to set listeners on separate images is because the inflated layout is dynamic. It can contain any number from 1 to 4 images. If I have a layout of two images then the fourth imageView listener would return a nullptr exception</p>