title: button with image is not responding to onclick listener when scrolling
body: <p>I am a android beginner, I have added images dynamically inside the linear layout, which will scroll from right to left. The problem is that the images are moving / scrolling properly but when I click on it the on-click listener is not called.</p>
  
  <p>Actually when moving the images the image button is moving but the onclick position is not changed according to the image scrolling.</p>
  
  <pre><code>public class ImageLayoutsActivity extends Activity {
  
      int ImageInt, fromXDelta, toXDelta;
      int PosInt, myHarizantalLayoutInt, aDisplayInt, aDisplayDiv = 0;
  
  
      AnimationSet myAnimation = new AnimationSet(true);
  
      LinearLayout myHarizantalLayout;
      HorizontalScrollView myHorizontalScroll;
  
      View myView;
      Intent myIntent;
  
      private Button clickedButton = null;
      public int currentimageindex = 0;
      TranslateAnimation myTranslateAnimation = null;
  
      private String[] imgArr = { "icn01", "icn02" };
  
      private String[] URLArray = { "http://www.google.co.in/",
              "http://in.yahoo.com/?p=us" };
  
      LinearLayout.LayoutParams Parems;
  
      Display aDisplay;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          View aView = LayoutInflater.from(GroupActivity.myGroup).inflate(
                  R.layout.horizantal, null);
          setContentView(aView);
  
          GroupActivity.myGroup.setTitle("---CII---");
  
          aDisplay = getWindowManager().getDefaultDisplay();
  
          aDisplayInt = aDisplay.getWidth();
  
          aDisplayDiv = aDisplayInt / 5;
  
          Log.i("value##########################", String.valueOf(aDisplayDiv));
  
          Log.i("aDisplayInt##########################",
                  String.valueOf(aDisplayInt));
  
  
  
  
  
          // define xml layout here
          myHarizantalLayout = (LinearLayout) findViewById(R.id.horiztonal_outer_layout_id);
  
          myHorizontalScroll = (HorizontalScrollView) findViewById(R.id.horiztonal_scrollview_id);
  
          // Button aBtnOne = (Button) findViewById(R.id.BtnOneId);
  
          // Hide HorizantalLayout scroll bar
          myHorizontalScroll.setHorizontalScrollBarEnabled(false);
  
          myHorizontalScroll.isSmoothScrollingEnabled();
  
  
          myHarizantalLayout.measure(aDisplay.getWidth(), aDisplay.getHeight());
  
          Log.v("EmailActivity@@@@@@@@@", myHarizantalLayout.getMeasuredHeight()
                  + ", " + myHarizantalLayout.getMeasuredWidth());
  
          int aLayoutInt = myHarizantalLayout.getMeasuredWidth();
  
          Log.i("aLayoutInt@@@@@@@@@@@", String.valueOf(aLayoutInt));
  
  
          // Add images in for loop
          for (int i = 0; i &lt; imgArr.length; i++) {
  
              // int aVal=myHarizantalLayout.getChildCount();
  
              Log.i("ImageInt@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "=====Inside the loop======");
  
              // create button instance
              final Button aImgBtn = new Button(this);
  
              // myButton.setOnClickListener(null);
  
              // add background image resource files
              ImageInt = getResources().getIdentifier(imgArr[i], "drawable",
                      getPackageName());
  
              Log.i("ImageInt@@@@@@@@@@@@@@@@@@@@@@@@@@@@", "ImageInt");
  
              // add integer image values to drawable control
              Drawable aDrawable = this.getResources().getDrawable(ImageInt);
  
              Log.i("aDrawable$$$$$$$$$$$$$$$$$$$$$$$$$", "aDrawable");
  
              // add drawable file to button instance
              aImgBtn.setBackgroundDrawable(aDrawable);
  
              aImgBtn.measure(aDisplay.getHeight(), aDisplay.getWidth());
  
              Log.i("aButton################@@@@@@@@@@@@@", "aButton");
  
              Parems = new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.WRAP_CONTENT,
                      LinearLayout.LayoutParams.WRAP_CONTENT);
  
              Parems.setMargins(10, 0, 5, 0);
  
              Log.i("Parems%%%%%%%%%%%%%%%%%%%%%%%%%%", "Parems");
  
              // int aParemIn = Parems.width;
  
              // Log.i("aparemIn$$$$$$$$$$$$", String.valueOf(aParemIn));
  
              myHarizantalLayout.measure(aDisplay.getHeight(),
                      aDisplay.getWidth());
  
              int myHarizantalLayoutInt = myHarizantalLayout.getMeasuredWidth();
  
              Log.i("myHarizantalLayoutInt$$$$$$$$$$$$",
                      String.valueOf(myHarizantalLayoutInt));
  
              myTranslateAnimation = new TranslateAnimation(aDisplayInt - 300,
                      -myHarizantalLayoutInt - aDisplayDiv, 0, 0);
              myTranslateAnimation.setDuration(20000);
              myTranslateAnimation.setRepeatCount(-1);
              myTranslateAnimation.setRepeatMode(1);
              myAnimation.addAnimation(myTranslateAnimation);
  
              Log.i("myAnimation###########################", "myAnimation");
  
  
              aImgBtn.setLayoutParams(Parems);
              myHarizantalLayout.addView(aImgBtn);
  
              Log.i("myHarizantalLayout&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;",
                      "myHarizantalLayout");
  
              // add animation to HarizantalLayout
              myHarizantalLayout.startAnimation(myTranslateAnimation);
  
              // Call webview based on image button click event
              aImgBtn.setOnClickListener(new OnClickListener() {
  
                  public void onClick(View view) {
  
                      Log.i("view$$$$$$$$$$$$@@@@@@@@@@@@@", "---btn clicked -----");
  
                      int aVal = myHarizantalLayout.indexOfChild(view);
  
                      Log.i("indexOfChild*************************",
                              "indexOfChild");
  
                      if (view == aImgBtn) {
  
                          Log.i("aButton%%%%%%%%%%%%%%%%%%%%%%%%", "aButton");
                          aImgBtn.setId(aVal);
  
                          String aUrlStr = URLArray[aVal];
  
                          Log.i("aUrlStr$$$$$$$$$$$$@@@@@@@@@@@@@", aUrlStr);
  
                      }
  
  
  
                      // Log.i("aUrlStr$$$$$$$$$$$$@@@@@@@@@@@@@", aUrlStr);
  
                      // Pass values to webview activity
  
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:gravity="bottom" android:background="#00f"&gt;
  
      &lt;LinearLayout android:layout_below="@+id/RelativeLayout"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:orientation="horizontal" android:gravity="bottom"&gt;
  
          &lt;ImageView android:id="@+id/barImageId"
              android:layout_width="150dp" android:layout_height="58dp" 
              android:background="@drawable/cii" android:layout_alignParentRight="true" /&gt;
  
          &lt;HorizontalScrollView android:layout_width="fill_parent"
              android:layout_height="58dp" android:id="@+id/horiztonal_scrollview_id"
              android:fadingEdge="none" android:background="#000" &gt;
  
              &lt;LinearLayout android:id="@+id/horiztonal_outer_layout_id"
                  android:fadingEdge="none" android:layout_height="fill_parent" android:gravity="center_vertical"
                  android:layout_width="fill_parent" android:background="#f00"&gt;
  
              &lt;/LinearLayout&gt;
          &lt;/HorizontalScrollView&gt;
  
      &lt;/LinearLayout&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>