Simple Table Builder for TableLayout for Android

Problem

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

Sample Code

//Building Table Header
TableLayout tv = TableBuilder.getCleanDefaultTableLayout(
            this, R.id.someID);
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);

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

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

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

    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:
tablebuilder_example

The TableBuilder

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

import android.app.Activity;
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);
        tv.removeAllViewsInLayout();
        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)
                b.setText(text);
            if(textColor > 0)
                b.setTextColor(textColor);
            if(textSize > 0)
                b.setTextSize(textSize);
            if(padding)
                b.setPadding(10, 0, 0, 0);
            tr.addView(b);
        }
    }

    //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);
            }
            tv.addView(tr);
        }
    }

    //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));
        vline.setBackgroundColor(color);
        tl.addView(vline);
    }
}
Advertisements