title: How do I fill a grid with ImageButtons programmatically?
body: <p>I'm new to Android development so I've been working on a chess app in order to teach myself. However, I am now stuck. Essentially, I need a good way to draw an 8x8 grid of ImageButtons such that:</p>
  
  <ol>
  <li>The buttons fill the entire grid</li>
  <li>Grid space is evenly distributed among the buttons</li>
  <li>There are no gaps between buttons (clarifying #2)</li>
  <li>The grid is square shaped and its size can be controlled (I'd like to be able to automatically resize based on screen size).</li>
  </ol>
  
  <p>Or, put simply, it should look like a standard chess board :)</p>
  
  <p>I've tried several layouts with no success. Currently, this is the best I've been able to do:</p>
  
  <p><img src="https://i.stack.imgur.com/Yxnrl.png" alt="&quot;Chessboard&quot;"></p>
  
  <p>I'm using a GridLayout in the following way inside of PlayGameActivity (Where the board should be drawn):</p>
  
  <pre><code>public void onStart(){
      super.onStart();
  
      board = (GridLayout)findViewById(R.id.chessboard);
      board.setColumnCount(8);
      board.setRowCount(8);
  
      for(int i = 0; i &lt; 8; i++){
          for(int j = 0; j &lt; 8; j++){
              ImageButton square = squares[i][j] = new ImageButton(this);
              GridLayout.LayoutParams params = new GridLayout.LayoutParams();
              params.rightMargin = 0;
              params.topMargin = 0;
              params.height = params.WRAP_CONTENT;
              params.width = params.WRAP_CONTENT;
              params.setGravity(Gravity.FILL);
              params.rowSpec = GridLayout.spec(i);
              params.columnSpec = GridLayout.spec(j);
  
              board.addView(square, params);
  
              //board.addView(square);
          }
      }}
  </code></pre>
  
  <p>And the chessboard is defined via XML like this:</p>
  
  <pre><code>&lt;GridLayout
      android:id="@+id/chessboard"
      android:layout_width="353dp"
      android:layout_height="353dp"
      android:layout_marginBottom="104dp"
      android:layout_marginEnd="8dp"
      android:layout_marginLeft="8dp"
      android:layout_marginRight="8dp"
      android:layout_marginStart="8dp"
      android:layout_marginTop="104dp"
      android:background="#000000"
      android:gravity="center_horizontal"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      tools:layout_constraintBottom_creator="1"
      tools:layout_constraintLeft_creator="1"
      tools:layout_constraintRight_creator="1"
      tools:layout_constraintTop_creator="1"&gt;
  &lt;/GridLayout&gt;
  </code></pre>
  
  <p>All solutions I've found for this type of problem have been in the form of Android XML. Maybe my concerns about such an approach are based on ignorance, but my understanding is that I'd have to copy and paste 64 squares into a GridLayout or something. Also, I'm not sure how I'd resize based on screen size with that approach. Ideally, I'm looking for a strictly programmatic solution (and this is my preferred method of UI design anyway).</p>