title: Android: Stretching rows in TableLayout programmatically
body: <p>I'm trying to create a TableLayout dynamically which has all its rows stretched like shown <a href="http://radioal.blogspot.com/2011/05/stretching-rows-in-android-tablelayout.html" rel="nofollow noreferrer">in this tutorial</a>. I've accomplished it via XML, but I'd like to do it from the Activity. Here is the code I've tried so far without success:</p>
  
  <pre><code>public View getTableWithAllRowsStretchedView() {
      LinearLayout linearLayout = new LinearLayout(this);
      linearLayout.setOrientation(LinearLayout.VERTICAL);
      linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
  
      TableLayout tableLayout = new TableLayout(this);
      tableLayout.setStretchAllColumns(true);
      tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.FILL_PARENT));
      tableLayout.setWeightSum(4);
  
  
      for (int i = 0; i &lt; 4; i++) {
          TableRow tableRow = new TableRow(this);
          tableRow.setGravity(Gravity.CENTER);
          tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT, 1.0f));
  
          for (int j = 0; j &lt; 4; j++) {
              Button button = new Button(this);
              final int buttonNumber = (j + i * 4);
              button.setText("" + buttonNumber);
              button.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
  
              tableRow.addView(button);
          }
          tableLayout.addView(tableRow);
      }
  
      linearLayout.addView(tableLayout);
      return linearLayout;
  }
  </code></pre>
  
  <p>Thank you very much in advance.</p>
  
  <p><strong><em>EDIT</em></strong>  </p>
  
  <pre><code>Screenshot of what I have
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/AIScP.png" alt="enter image description here"></p>
  
  <pre><code>what I expect
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/kuGAZ.png" alt="enter image description here"></p>