Skip to content

Instantly share code, notes, and snippets.

@kitek

kitek/Table.java

Created Oct 6, 2013
Embed
What would you like to do?
Dynamic TableLayout
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
TableLayout table = new TableLayout(getActivity());
table.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int i = 0; i < 10; i++) {
TableRow row = new TableRow(getActivity());
row.setWeightSum(2.0f);
row.setPadding(1, 1, 1, 1);
TableLayout.LayoutParams params = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1f);
for (int j = 0; j < 2; j++) {
TableRow.LayoutParams textViewParams = new TableRow.LayoutParams();
TextView cell = new TextView(getActivity());
cell.setText("cell [" + i + ", " + j + "]");
cell.setPadding(6, 4, 6, 4);
cell.setLayoutParams(textViewParams);
row.addView(cell);
}
row.setLayoutParams(params);
table.addView(row, params);
}
table.setStretchAllColumns(true);
linearLayout.addView(table);
return linearLayout;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.