generate Table Layout - Android User Interface

Android examples for User Interface:Layout

Description

generate Table Layout

Demo Code


//package com.java2s;
import android.content.Context;
import android.view.Gravity;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

public class Main {
    public static void generateTableLayout(TableLayout layout,
            Context context, int rowCount, int colCount, String text) {
        layout.removeAllViews();//from   w w  w .  j  a v  a2 s .c  om

        for (int j = 0; j < rowCount; ++j) {
            TableRow row = new TableRow(context);
            row.setGravity(Gravity.CENTER);

            for (int i = 0; i < colCount; ++i) {
                Button btn = new Button(context);
                btn.setText(text);
                btn.setGravity(Gravity.CENTER);
                row.addView(btn, new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.MATCH_PARENT, 0.5f));
            }

            layout.addView(row, new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.MATCH_PARENT, 1.f));
        }

    }
}

Related Tutorials