AI2 Component  (Version nb184)
TableLayout.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
11 
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;
19 
25 @SimpleObject
26 public class TableLayout implements Layout {
27 
28  private final android.widget.TableLayout layoutManager;
29  private final Handler handler;
30 
31  private int numColumns;
32  private int numRows;
33 
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();
44 
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());
49  }
50  layoutManager.addView(tableRow, row, new android.widget.TableLayout.LayoutParams());
51  }
52  }
53 
54  int getNumColumns() {
55  return numColumns;
56  }
57 
58  void setNumColumns(int newNumColumns) {
59  if (newNumColumns > numColumns) {
60  // Add new cells in each row.
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());
66  }
67  }
68  numColumns = newNumColumns;
69 
70  } else if (newNumColumns < numColumns) {
71  // Remove extra cells from each row.
72  for (int row = 0; row < numRows; row++) {
73  TableRow tableRow = (TableRow) layoutManager.getChildAt(row);
74  tableRow.removeViews(newNumColumns, numColumns - newNumColumns);
75  }
76  numColumns = newNumColumns;
77  }
78  }
79 
80  int getNumRows() {
81  return numRows;
82  }
83 
84  void setNumRows(int newNumRows) {
85  if (newNumRows > numRows) {
86  // Add new rows
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());
92  }
93  layoutManager.addView(tableRow, row, new android.widget.TableLayout.LayoutParams());
94  }
95  numRows = newNumRows;
96  } else if (newNumRows < numRows) {
97  // Remove extra rows
98  layoutManager.removeViews(newNumRows, numRows - newNumRows);
99  numRows = newNumRows;
100  }
101  }
102 
103  // Layout implementation
104 
105  public ViewGroup getLayoutManager() {
106  return layoutManager;
107  }
108 
109  public void add(AndroidViewComponent child) {
110  // At this time, the child doesn't have its row and column properties. So,
111  // we'll actually add the child later.
112  // However, we need to create the layout parameters for the child's view because the width and
113  // height properties will be set before the child is actually added to the table and setting
114  // the width and height properties requires that the child's view has layout parameters.
115  child.getView().setLayoutParams(newCellLayoutParams());
116  addChildLater(child);
117  }
118 
119  /*
120  * Causes addChild to be called later.
121  */
122  private void addChildLater(final AndroidViewComponent child) {
123  handler.post(new Runnable() {
124  public void run() {
125  addChild(child);
126  }
127  });
128  }
129 
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);
136 
137  } else {
138 
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());
145  } else {
146  Log.e("TableLayout", "Child has illegal Column property: " + child);
147  }
148  } else {
149  Log.e("TableLayout", "Child has illegal Row property: " + child);
150  }
151  }
152  }
153 
154  private View newEmptyCellView() {
155  return new TextView(layoutManager.getContext());
156  }
157 
158  private static TableRow.LayoutParams newEmptyCellLayoutParams() {
159  return new TableRow.LayoutParams(0, 0);
160  }
161 
162  private static TableRow.LayoutParams newCellLayoutParams() {
163  return new TableRow.LayoutParams();
164  }
165 }
com.google.appinventor.components.runtime.Layout
Definition: Layout.java:16
com.google.appinventor.components
com.google.appinventor.components.runtime.TableLayout
Definition: TableLayout.java:26
com.google.appinventor.components.runtime.TableLayout.getLayoutManager
ViewGroup getLayoutManager()
Definition: TableLayout.java:105
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.AndroidViewComponent.getView
abstract View getView()
com.google
com
com.google.appinventor.components.common.ComponentConstants
Definition: ComponentConstants.java:13
com.google.appinventor.components.runtime.AndroidViewComponent
Definition: AndroidViewComponent.java:27
com.google.appinventor.components.runtime.TableLayout.add
void add(AndroidViewComponent child)
Definition: TableLayout.java:109
com.google.appinventor.components.annotations
com.google.appinventor