Simple Table Builder for TableLayout for Android


You want to build a simple table in an Android App.

Sample Code

//Building Table Header
TableLayout tv = TableBuilder.getCleanDefaultTableLayout(
TableRow tr = TableBuilder.getDefaultTableRow(this);

String[] ids = {"Column1", "Column2", "Column3", 
            "Column4", "Column5"};
int[] colors = {Color.BLUE, Color.BLUE, Color.BLUE, 
            Color.BLUE, Color.BLUE};
int[] sizes = {15, 15, 15, 15, 15};

TableBuilder.addTextViewsAsTableRow(this, tv, ids, 
            colors, sizes, true);

TableBuilder.addLineToTable(this, tv, Color.WHITE);

List<T> list = dbMgr.getSomeThing();
Iterator it = list.iterator();

//data rows
while(it.hasNext()) {
    T t = (T);

    tr = TableBuilder.getDefaultTableRow(this);

    //change here row input
    String[] id = {t.get1(), t.get2(), t.get3(), 
                t.get4(), t.get5()};
    int[] color = {Color.RED, Color.WHITE, Color.WHITE, 
                Color.WHITE, Color.WHITE};
    int[] size = {15, 15, 15, 15, 15};

    TableBuilder.addTextViewsAsTableRow(this, tv, id, 
                color, size, true);
    TableBuilder.addLineToTable(this, tv, Color.BLUE);

The result looks like this:

The TableBuilder

You can also download the code from my playground GitHub repository.

import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TableBuilder
    //create TableLayout for id
    public static TableLayout getDefaultTableLayout(Activity activity, int id)
        TableLayout tv = (TableLayout) activity.findViewById(id);
        return tv;

    //create TableLayout for id and remove all old views (reset)
    public static TableLayout getCleanDefaultTableLayout(Activity activity, int id)
        TableLayout tv = (TableLayout) activity.findViewById(id);
        return tv;

    //createn default TableRow
    public static TableRow getDefaultTableRow(Activity activity)
        TableRow tr = new TableRow(activity);
        tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        return tr;

    //add textview to row
    public static void addTextViewToTableRow(Activity activity, TableRow tr, String text, int textColor, int textSize, boolean padding)
        if(activity != null && tr != null) {
            TextView b = new TextView(activity);
            if(text != null)
            if(textColor > 0)
            if(textSize > 0)
                b.setPadding(10, 0, 0, 0);

    //add multiple textviews to row
    public static void addTextViewsAsTableRow(Activity activity, TableLayout tv, String[] texts, int[] colors, int[] sizes, boolean padding)
        if(activity != null && tv != null && texts.length==colors.length && texts.length==sizes.length)
            TableRow tr = getDefaultTableRow(activity);
            for(int i = 0; i < texts.length; i++) {
                addTextViewToTableRow(activity, tr, texts[i], colors[i], sizes[i], padding);

    //add a horizontal line to the table (used to separate two rows)
    public static void addLineToTable(Activity activity, TableLayout tl, int color)
        final View vline = new View(activity);
        vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 2));

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s