title: Android: Programmatically Setting width of Child View using Percentage
body: <p>I've read this article:</p>
  
  <p><a href="http://andmobidev.blogspot.com/2010/01/setting-width-of-view-using-percentage.html" rel="nofollow">Setting width of Child View using Percentage</a></p>
  
  <p>So far it works great if we set the weight and width in XML but I want to do it programmatically. I've a table in my layout (XML) and I am adding rows to it programmatically. In each row, I am adding 2 Text Views. I tried doing like this:</p>
  
  <pre><code>TableLayout tl = (TableLayout)findViewById(R.id.myTable);
  for(int i = 0; i &lt; nl.getLength(); i++){
              NamedNodeMap np = nl.item(i).getAttributes();
                  TableRow trDetails = new TableRow(this);
                  trDetails.setBackgroundColor(Color.BLACK);
                  TextView tvInfo = new TextView(this);
                  tvInfo.setTextColor(Color.WHITE);
                  tvInfo.setGravity(Gravity.LEFT);
                  tvInfo.setPadding(3, 3, 3, 3);
                  tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
  
                  tvInfo.setText("info");
  
                  TextView tvDetails = new TextView(this);
                  tvDetails.setTextColor(Color.WHITE);
                  tvDetails.setGravity(Gravity.LEFT);
                  tvDetails.setPadding(3, 3, 3, 3);
                  tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
                  tvDetails.setText(np.getNamedItem("d").getNodeValue());
  
                  trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
                  trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
  
                  tl.addView(trDetails,new TableLayout.LayoutParams(
                          LayoutParams.FILL_PARENT,
                          LayoutParams.WRAP_CONTENT));
  }
  </code></pre>
  
  <p>but with no luck, it shows blank screen but if I do it in XML then it shows perfectly. Please help me sorting out this issue where I am doing wrong?</p>
  
  <p>Thanks</p>