title: How to store data in ImageView in android dynamically?
body: <p>Subject of interest : Android</p>
  <p>After hours of searching I couldn't find or understand how to create ImageView dynamically to store associative data and perform an action depending on the image view clicked.</p>
  <p>Let me elaborate ,
  I am creating a grid of ImageViews using GridLayout (as gridview examples are too complex for me). I am able to create the grid but still struggling with alignment issues.
  So now I have this Grid of ImageViews, but from what I learned, the Id's of these ImageViews are converted to integers at run time. But I need these IDs as I use them to fetch data from server depending on their uniqueness. Is there a way to store other data than the Ids in ImageView Tags?. I come from a web development background where I could use JavaScript easily to create elements with dynamic Ids to refer later and use in queries. So please guide me.</p>
  <p><strong>UPDATE:</strong>
  I tried using the SetId method, and that is when I got this doubt. Because SetId only lets me set IntegerIDs. What if I needed alphanumeric ID to be used later for querying purposes?.</p>
  <p><strong>Exam_Screen.Java</strong></p>
  <pre><code>public class Exam_Screen extends AppCompatActivity {
  
  GridLayout gridLayout;
  public int[] QArray;
  
  private int[] GetQuestionsFromServer() {
      return new int[50];
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_exam__screen);
      QArray = GetQuestionsFromServer();
      AddQuestionsToScrollArea(QArray);
  }
  public void AddQuestionsToScrollArea(int[] QArray)
  {
      gridLayout = findViewById(R.id.gridlayout);
      gridLayout.removeAllViews();
  
      int total = QArray.length;
      int column = 4 ;
      int row = total / column;
      gridLayout.setColumnCount(column);
      gridLayout.setRowCount(row + 1);
      //gridLayout
      for (int i = 0, c = 0, r = 0; i &lt; total; i++, c++) {
          if (c == column) {
              c = 0;
              r++;
          }
          ImageView oImageView = new ImageView(this);
          oImageView.setScaleX(0.3f);
          oImageView.setScaleY(0.3f);
          oImageView.setId(i); // I want to set the Ids I get from Server, and use them later
          oImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
          oImageView.setAdjustViewBounds(true);
          oImageView.setImageResource(R.drawable.new_candidate_image);
  
          GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
          lp.width = 30;
          lp.height = 30;
          lp.topMargin = 5;
          lp.bottomMargin = 5;
          lp.leftMargin = 5;
          lp.rightMargin = 5;
  
          oImageView.setLayoutParams(lp);
  
          GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
          GridLayout.Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
          if (r == 0 &amp;&amp; c == 0) {
              Log.e(&quot;&quot;, &quot;spec&quot;);
              Log.d(&quot;Column&quot;, &quot;value: &quot; + column);
              Log.d(&quot;rows&quot;, &quot;value: &quot; + row);
              colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
              rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
          }
  
          GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                  rowSpan, colspan);
          gridLayout.addView(oImageView, gridParam);
      }
  }
  }
  </code></pre>
  <p><strong>Relevant Exam Screen Activity XML</strong></p>
  <pre><code>&lt;ScrollView
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:background=&quot;@drawable/border&quot;&gt;
      &lt;GridLayout
           android:id=&quot;@+id/gridlayout&quot;
           android:layout_width=&quot;match_parent&quot;
           android:layout_height=&quot;match_parent&quot;/&gt;
  &lt;/ScrollView&gt;
  
  </code></pre>