title: Programmatic Android Layout
body: <p>I'm trying to correlate how the Android XML looks versus creating it programmatically and I'm failing miserably. I've read several references stating that the type for the LayoutParams for the View must be equal to those of the parent, but I'm just not grokking it. </p>
  
  <p>Given the following XML, how would I re-create this programmatically?</p>
  
  <pre><code>&lt;TableRow
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:layout_gravity="center_vertical"
  &gt;
      &lt;LinearLayout  
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:layout_gravity="center_vertical"
          android:paddingTop="3dp"
          android:orientation="vertical"
      &gt;
          &lt;EditText 
              android:padding="2dp"
              android:layout_width="100dp"
              android:layout_height="35dp"
              android:layout_column="0"
              android:layout_weight="1"
              android:layout_gravity="center_vertical"
              android:textSize="15dp"
              android:numeric="integer"
          /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout 
          android:layout_height="35dp"
          android:layout_width="fill_parent"
          android:layout_weight="1"
          android:layout_gravity="center_vertical"
          android:padding="2dp"
          android:orientation="vertical"
          &gt;
  
          &lt;CheckBox android:text="Check1" 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" 
              android:layout_weight="1"
              android:textSize="10sp" 
              android:background="@drawable/checkbox_background" 
              android:button="@drawable/checkbox" 
              android:textColor="@color/Gray"
          /&gt;
          &lt;CheckBox android:text="Check2" 
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:textSize="10sp" 
              android:background="@drawable/checkbox_background" 
              android:button="@drawable/checkbox" 
              android:textColor="@color/Gray"
          /&gt;
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here's what I've tried:</p>
  
  <pre><code>// Get the TableLayout
  TableLayout tl = (TableLayout)findViewById(R.id.score_table);
  
  LayoutParams lp;
  TableRow.LayoutParams tp;
  
  // Create a TableRow and give it an ID
  TableRow tr = new TableRow(this);
  tr.setId(100);
  tr.setLayoutParams(new TableRow.LayoutParams(
          LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  
  LinearLayout ll0_1 = new LinearLayout(this);
  // If the layout params of the child should be of
  // the type of the parent this should be 
  // TableRow.LayoutParams?
  tp = (TableRow.LayoutParams)tr.getLayoutParams();
  tp.height = TableRow.LayoutParams.WRAP_CONTENT;
  tp.width = TableRow.LayoutParams.FILL_PARENT;
  tp.gravity = Gravity.CENTER_VERTICAL;
  // TableRow.LayoutParams has no padding so this
  // gets set on the LinearLayout?
  ll0_1.setPadding(0,3,0,0);
  ll0_1.setLayoutParams(tp);
  tr.addView(ll0_1);
  
  EditText et0 = new EditText(this);
  et0.setId(100);
  et0.setInputType(InputType.TYPE_CLASS_NUMBER);
  // Same as above; LP set to type of parent
  lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
          LayoutParams.WRAP_CONTENT, 1f);
  lp.height = 35;
  lp.width = 100;
  // Same as above; the parent's type doesn't have 
  // these attributes
  et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
  et0.setPadding(0, 3, 0, 0);
  et0.setLayoutParams(lp);
  ll0_1.addView(et0);
  
  LinearLayout ll0_2 = new LinearLayout(this);
  ll0_2.setOrientation(LinearLayout.VERTICAL);
  tr.addView(ll0_2);
  
  CheckBox cb0_1 = new CheckBox(this);
  cb0_1.setTextSize(10f);
  cb0_1.setTextColor(Color.GRAY);
  cb0_1.setBackgroundResource(R.drawable.checkbox_background);
  cb0_1.setButtonDrawable(R.drawable.checkbox);
  cb0_1.setText("Nil");
  ll0_2.addView(cb0_1);
  
  CheckBox cb0_2 = new CheckBox(this);
  cb0_2.setTextSize(10f);
  cb0_2.setTextColor(Color.GRAY);
  cb0_2.setBackgroundResource(R.drawable.checkbox_background);
  cb0_2.setButtonDrawable(R.drawable.checkbox);
  cb0_2.setText("10fer");
  ll0_2.addView(cb0_2);
  
  // Duplicate code removed for brevity
  //...
  
  // Add the TableRow to the TableLayout
  tl.addView(tr, new TableLayout.LayoutParams(
          LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  </code></pre>
  
  <p>There is no error, but it does not match the XML. Instead, it looks like this:
  <img src="https://i.stack.imgur.com/cfSba.png" alt="enter image description here"></p>
  
  <p>The first one is from the XML, the second one is my attempt at reproducing it programmatically. </p>