title: Grid Layout Add Evenly Spaced Images
body: <p>I am trying to fit a specified amount of images in a Grid Layout programmatically and I want them all to be evenly sized and spread out. Whenever I try to add multiple images the grid layout only shows one. What am I doing wrong?</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;GridLayout
          android:id="@+id/gridlayout"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="6"
          android:gravity="center"/&gt;
  </code></pre>
  
  <p>Java: </p>
  
  <pre><code>//reset data and grid
      data.clear();
      gridLayout.removeAllViews();
  
      Random random = new Random();
      for(int i = 0; i &lt; num; i++){
          data.add(String.format("%." + 0 + "f", random.nextDouble() * (6 - 1) + 1));
          ImageView imageView = new ImageView(getContext());
          switch (data.get(i)) {
              case "1":
                  imageView.setImageResource(R.drawable.dice1);
                  break;
              case "2":
                  imageView.setImageResource(R.drawable.dice2);
                  break;
              case "3":
                  imageView.setImageResource(R.drawable.dice3);
                  break;
              case "4":
                  imageView.setImageResource(R.drawable.dice4);
                  break;
              case "5":
                  imageView.setImageResource(R.drawable.dice5);
                  break;
              default:
                  imageView.setImageResource(R.drawable.dice6);
                  break;
          }
          gridLayout.addView(imageView, i);
      }
  </code></pre>
  
  <p>What shows up:
  <a href="https://i.stack.imgur.com/TbeI7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TbeI7.png" alt="enter image description here"></a></p>
  
  <p>What I want:
  <a href="https://i.stack.imgur.com/tMQAa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tMQAa.jpg" alt="enter image description here"></a></p>