title: Get id of an ImageView in Scrollview in Android
body: <p>How can I get the id of an element that has been clicked, if this element is found in a scrollView? In my case I have 4 photos (ImageViews) and if I click on one of them, I want to get its <strong>id</strong> (already set with ImageView.setId(int) ). Here is my code:</p>
  
  <pre><code>package scroll.s;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.HorizontalScrollView;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  public class ScrollsActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          int layoutId = 0;  
  
          RelativeLayout main = new RelativeLayout(this);
          RelativeLayout.LayoutParams mainParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
          main.setId(layoutId);
          layoutId++;
  
          TextView past = new TextView (this);
          RelativeLayout.LayoutParams pastParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
          past.setId(layoutId);
          layoutId++;
          past.setText("Past: ");
          pastParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
          main.addView(past, pastParams);
  
          final HorizontalScrollView hsv = new HorizontalScrollView(this);
          hsv.setId(layoutId);
          layoutId++;
          hsv.setHorizontalScrollBarEnabled(false);
          mainParams.addRule(RelativeLayout.BELOW, 1);
          main.addView(hsv, mainParams);        
  
          final RelativeLayout relative1 = new RelativeLayout(this);
          relative1.setId(layoutId);
          layoutId++;
          hsv.addView(relative1);
  
          for (int i=0; i&lt;4; i++) {
              ImageView current = new ImageView(this);
              current.setBackgroundDrawable(getResources().getDrawable(R.drawable.example));
              current.setId(layoutId);
  
              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
              params.addRule(RelativeLayout.RIGHT_OF, (layoutId-1) );
              params.leftMargin=10;
              relative1.addView(current, params);
              layoutId++;
          }
  
          for (int i=0; i&lt;4; i++) {
              TextView currentText = new TextView(this);
              currentText.setText("random text");
              currentText.setId(layoutId);
  
              RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
              params.addRule(RelativeLayout.BELOW, (layoutId-4) );
              params.addRule(RelativeLayout.ALIGN_LEFT, (layoutId-4) );
              params.topMargin=5;
              relative1.addView(currentText, params);
              layoutId++;
          }
  
  
          this.setContentView(main);   
      }
  }
  </code></pre>
  
  <p>Here is what I achieve with this code : <img src="https://i.stack.imgur.com/oihTc.png" alt="scrollsview"></p>
  
  <p>I get the <strong>ERROR/AndroidRuntime(2258): android.content.res.Resources$NotFoundException: String resource ID #0x4</strong>
  when I put the following code:</p>
  
  <pre><code>    current.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              Toast.makeText(getBaseContext(), current.getId(), Toast.LENGTH_SHORT).show();
          }
      });
  </code></pre>