7 package com.google.appinventor.components.runtime;
12 import android.content.Context;
13 import android.os.Handler;
14 import android.util.Log;
15 import android.view.View;
16 import android.view.ViewGroup;
17 import android.widget.TableRow;
18 import android.widget.TextView;
28 private final android.widget.TableLayout layoutManager;
29 private final Handler handler;
31 private int numColumns;
39 TableLayout(Context context,
int numColumns,
int numRows) {
40 layoutManager =
new android.widget.TableLayout(context);
41 this.numColumns = numColumns;
42 this.numRows = numRows;
43 handler =
new Handler();
45 for (
int row = 0; row < numRows; row++) {
46 TableRow tableRow =
new TableRow(context);
47 for (
int col = 0; col < numColumns; col++) {
48 tableRow.addView(newEmptyCellView(), col, newEmptyCellLayoutParams());
50 layoutManager.addView(tableRow, row,
new android.widget.TableLayout.LayoutParams());
58 void setNumColumns(
int newNumColumns) {
59 if (newNumColumns > numColumns) {
61 Context context = layoutManager.getContext();
62 for (
int row = 0; row < numRows; row++) {
63 TableRow tableRow = (TableRow) layoutManager.getChildAt(row);
64 for (
int col = numColumns; col < newNumColumns; col++) {
65 tableRow.addView(newEmptyCellView(), col, newEmptyCellLayoutParams());
68 numColumns = newNumColumns;
70 }
else if (newNumColumns < numColumns) {
72 for (
int row = 0; row < numRows; row++) {
73 TableRow tableRow = (TableRow) layoutManager.getChildAt(row);
74 tableRow.removeViews(newNumColumns, numColumns - newNumColumns);
76 numColumns = newNumColumns;
84 void setNumRows(
int newNumRows) {
85 if (newNumRows > numRows) {
87 Context context = layoutManager.getContext();
88 for (
int row = numRows; row < newNumRows; row++) {
89 TableRow tableRow =
new TableRow(context);
90 for (
int col = 0; col < numColumns; col++) {
91 tableRow.addView(newEmptyCellView(), col, newEmptyCellLayoutParams());
93 layoutManager.addView(tableRow, row,
new android.widget.TableLayout.LayoutParams());
96 }
else if (newNumRows < numRows) {
98 layoutManager.removeViews(newNumRows, numRows - newNumRows);
106 return layoutManager;
115 child.
getView().setLayoutParams(newCellLayoutParams());
116 addChildLater(child);
123 handler.post(
new Runnable() {
130 private void addChild(AndroidViewComponent child) {
131 int row = child.Row();
132 int col = child.Column();
133 if (row == ComponentConstants.DEFAULT_ROW_COLUMN ||
134 col == ComponentConstants.DEFAULT_ROW_COLUMN) {
135 addChildLater(child);
139 if (row >= 0 && row < numRows) {
140 if (col >= 0 && col < numColumns) {
141 TableRow tableRow = (TableRow) layoutManager.getChildAt(row);
142 tableRow.removeViewAt(col);
143 View cellView = child.getView();
144 tableRow.addView(cellView, col, cellView.getLayoutParams());
146 Log.e(
"TableLayout",
"Child has illegal Column property: " + child);
149 Log.e(
"TableLayout",
"Child has illegal Row property: " + child);
154 private View newEmptyCellView() {
155 return new TextView(layoutManager.getContext());
158 private static TableRow.LayoutParams newEmptyCellLayoutParams() {
159 return new TableRow.LayoutParams(0, 0);
162 private static TableRow.LayoutParams newCellLayoutParams() {
163 return new TableRow.LayoutParams();