title: How to set layout_columnWeight for a GridLayout child when added programmatically?
body: <p>I'd like to use a <code>GridLayout</code> (not <code>GridView</code>) as board for a game like chess or checkers. As I'm a little reluctant to use an xml file with 64 child <code>View</code>s, I've tried adding them programmatically. </p>
  
  <p>To keep things simple, I started with using <code>TextView</code>s as child <code>View</code>s for the <code>GridLayout</code>.</p>
  
  <p>My problem is that the <code>View</code>s are not distributed evenly, and that I don't know how to get an even distribution in my java code. There is no method like "setWeight()" for setting <code>layout_columnWeight</code> and <code>layout_rowWeight</code>.</p>
  
  <p>At present, this is my <strong>activity_dynamic_grid_layout.xml</strong>:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
  &lt;ImageView
      android:layout_width="match_parent"
      android:layout_height="80dp"
      android:id="@+id/ivLogo"
      android:background="#ff0000"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"/&gt;
  
  &lt;android.support.v7.widget.GridLayout
      android:id="@+id/grid_layout"
      android:background="#004080"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/ivLogo"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="36dp"&gt;
  
  &lt;/android.support.v7.widget.GridLayout&gt;
  </code></pre>
  
  <p>I've set the <code>GridLayout</code> width and height to <code>match_parent</code> here, but I'm changing them at runtime using a <code>ViewTreeObserver.OnPreDrawListener</code> in order to get a square board. This works, the colored background is showing a square space as intended.</p>
  
  <p>My <strong>onCreate()</strong></p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_dynamic_grid_layout);
  
      GridLayout gl = (GridLayout) findViewById(R.id.grid_layout);
      gl.setColumnCount(8);
      gl.setRowCount(8);
  
      for(int i=0; i&lt;gl.getRowCount(); i++)
      {
          GridLayout.Spec rowSpec = GridLayout.spec(i, 1, GridLayout.FILL);
  
          for(int j=0;j&lt;gl.getColumnCount();j++)
          {
              GridLayout.Spec colSpec = GridLayout.spec(j,1, GridLayout.FILL);
  
              TextView tvChild = new TextView(this);
              tvChild.setText("[ " + i + " | " + j + " ]");
              tvChild.setTextSize(18f);
              tvChild.setTextColor(Color.WHITE);
              tvChild.setGravity(Gravity.CENTER);
  
              GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
              myGLP.rowSpec = rowSpec;
              myGLP.columnSpec = colSpec;
  
              gl.addView(tvChild, myGLP );
          }
      }
  
      final View rootView = findViewById(R.id.dynamic_root);
  
      rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
      {
          @Override
          public boolean onPreDraw()
          {
              int w = rootView.getMeasuredWidth();
              int h = rootView.getMeasuredHeight();
              int min = Math.min(w, h);
  
              ViewGroup.LayoutParams lp = gl.getLayoutParams();
              lp.width = min - min % 9;
              lp.height = lp.width;
              gl.setLayoutParams(lp);
  
              rootView.getViewTreeObserver().removeOnPreDrawListener(this);
  
              return true;
          }
      });
  }
  </code></pre>
  
  <p><strong>What I've tried already:</strong></p>
  
  <p>I put one <code>TextView</code> child in the layout file and tried to copy the <code>layout_columnWeight</code> and <code>layout_rowWeight</code> from its <code>GridLayout.LayoutParams</code>:</p>
  
  <pre><code> &lt;android.support.v7.widget.GridLayout
   ...&gt;   
      &lt;TextView
          android:id="@+id/clone_my_params"
          android:text="[ 0 | 0 ]"
          android:textColor="#ffffff"
          android:textSize="18sp"
          app:layout_column="0"
          app:layout_row="0"
          app:layout_columnWeight="1"
          app:layout_rowWeight="1"
      /&gt;
  &lt;/android.support.v7.widget.GridLayout&gt;
  </code></pre>
  
  <p>Additional code in <code>onCreate()</code>, before the double loop:</p>
  
  <pre><code>TextView v = (TextView)gl.findViewById(R.id.clone_my_params);
  v.setGravity(Gravity.CENTER);
  GridLayout.LayoutParams gridLayoutParamsToCopy = new GridLayout.LayoutParams(v.getLayoutParams());
  </code></pre>
  
  <p>Inside the loop, I skipped (i,j) = (0,0) and changed</p>
  
  <pre><code> GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams();
  </code></pre>
  
  <p>to</p>
  
  <pre><code> GridLayout.LayoutParams myGLP = new GridLayout.LayoutParams(gridLayoutParamsToCopy);
  </code></pre>
  
  <p>Before the change, all elements were in the upper left corner, the excess space was given to the last row/ column. After the change, the first row/ column had the excess space, no change for the other elements.</p>
  
  <p>Calling <code>gl.invalidate()</code> and/or <code>gl.requestLayout()</code> after the double loop had no effect.</p>
  
  <p>So it seems that I did not manage to set the desired weight by using the copy constructor.</p>