user1004816 user1004816 - 1 year ago 69
Android Question

Table columns in Android's TableLayout

Does anyone know whether it is possible to add columns to a TableLayout in Android? I need to display a relatively huge amount of data within a table. Obviously, this shouldn't be displayed all in one column.

You can add rows by creating a TableRow()-Object. Furthermore there's some kinda xml-parameter, but I don't know how to access it via the Java-Code.

TextView val = new TextView(this);
table.addView(tr,new TableLayout.LayoutParams(

I was unable to find a matching value in the TableRow.LayoutParams that worked for me. But probably I tried to use it in the wrong way.

table.addView(val,new TableRow.LayoutParams(
LayoutParams. ?? ));

Answer Source

As you add new views to the tablet row each view becomes a new "column".

So, if you do something like this (assuming your row data is in List rowData and your column data is in a List in rowData.

for (RowData rd : rowData)
    TableRow row = new TableRow(this);
    for (DataPoint dp : rd.getDataPoints())
       TextView tv = new TextView(this);

       /* any other styling stuff here */


That will result in a table, with multiple columns, all within a single table.

For layout params, you can use:

new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, (float) 5);

What that means is 0 width, wrap content on height, then a relative "weight" for the width. Android will layout your columns nicely with this.

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