title: SetonClickListerner is not working if I add Views to LinerLayout Programmatically using For Loop in Android
body: <p><strong>What I am doing:</strong></p>
  
  <p>I am adding TextView and Imageview  to LinearLayout Programmatically using For Loop[code is added below]</p>
  
  <p><strong>What Output and Issue I am Getting :</strong></p>
  
  <p><img src="https://i.stack.imgur.com/ENixM.png" alt="enter image description here"></p>
  
  <p><strong>What I want :</strong>
      I want to implement "setOnClickListener" for all imageviews</p>
  
  <p><strong>Problem I am Getting :</strong> 
      If I Click on last imageview[5th one],setOnClickListener is working,But For Remaining it is not working,Could anyone help me to rectify this problem?</p>
  
  <p><strong>Codind and XML :</strong> </p>
  
  <p><strong>Xml  : Layout Name is : car</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" &gt;
      &lt;TextView
          android:id="@+id/carName"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dp"
          android:layout_weight="1"
          android:padding="10dp"
          android:text="@string/string_mmed"
          android:textColor="#444"
          android:gravity="center"
          android:textSize="15sp" /&gt;
   &lt;TextView
          android:id="@+id/carType"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginTop="5dp"
          android:layout_weight="1"
          android:padding="10dp"
          android:text="@string/mmed_type"
          android:textColor="#444"
          android:gravity="center"
          android:textSize="15sp" /&gt;
        &lt;ImageView
                  android:id="@+id/carImageView"
                  android:layout_width="0dp"
                  android:layout_height="wrap_content"
                  android:layout_weight="0.2"
                  android:src="@drawable/info" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>Coding  : //Declartion</strong></p>
  
  <pre><code>TextView carName,carType;
  ImageView carImageView
  LinearLayout carLinearLayout;
  
  oncreate(){
      carLinearLayout= (LinearLayout) findViewById(R.id.carLinearLayout);
  }
  </code></pre>
  
  <p><strong>//To Iterate and displayview 5 times</strong></p>
  
  <pre><code>for (int i = 0; i &lt; 5; i++) {
              View child = getActivity().getLayoutInflater().inflate(R.layout.car, null);
              getCarViews(child);
              carName.setText("geting from getter setter");
              carType.setText("geting from getter setter");
              carLinearLayout.addView(child);
  }
   carImageView.setOnClickListener(this);
  </code></pre>
  
  <p><strong>//Intialization</strong></p>
  
  <pre><code>getCarViews(View child){
  carName= (TextView) child.findViewById(R.id.carName);
  carType= (TextView) child.findViewById(R.id.carType);
  carImageView= (ImageView) child.findViewById(R.id.carImageView);
  }
  </code></pre>