Afa Afa -5 years ago 108
Android Question

dynamically added table is not displayed in android

there is something going wrong , such that firstly i created separately xml file for table row ,it did not work, then i created table row and text view programatically, again it is not displayed when i run , i can not get if what is wrong with my code

table = (TableLayout) findViewById(;
for (int i = 0; i < attrName.length; i++) {

TableRow tr = new TableRow(getApplicationContext());
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

TextView atName = new TextView(getApplicationContext());

table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));


Answer Source

Try This it may be help to you

for (int i = 0; i < attrName.length; i++) {

         TableRow tr = new TableRow(this);
         tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,   TableRow.LayoutParams.WRAP_CONTENT));

         TextView atName = new TextView(this);
         atName.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
         table.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

You must apply LayoutParam for TextView and for create new Object for TableRow or TextView you should use Activity Context not ApplicationContext

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download