[ACCEPTED]-Create TableLayout programmatically-tablelayout

Accepted answer
Score: 41

Just to make the answer more clear:

TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

TableLayout tableLayout = new TableLayout(context);
tableLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));// assuming the parent view is a LinearLayout

TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(tableParams);// TableLayout is the parent view

TextView textView = new TextView(context);
textView.setLayoutParams(rowParams);// TableRow is the parent view


When 2 you call setLayoutParams, you are supposed to pass the 1 LayoutParams of the parent view

Score: 6

It turns out I needed to specify TableRowLayout, TableLayout 2 etc for the layout params, otherwise the 1 table just won't show!

Score: 1

A good solution is to inflate layout files 2 for each instance of row you want to create. See 1 this post : How to duplicate Views to populate lists and tables?

Score: 0

For me, to get mine I had to call addContentView().


Score: 0

Your problem is at this line:

b.setLayoutParams(new LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

You need to 1 change LayoutParams to TableRow.LayoutParams:

b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

More Related questions