In an Android app, I am trying to dynamically populate a TableLayout from an array, and I want each TableRow to hold at maximum 3 elements.
The way I had to implement it (code below), feels a lot like dynamically creating HTML tables and I was wondering if Android had more ad-hoc layout facilities that don't make me feel as if I am forcing my design idea over a more generic Layout like TableLayout.
Ideally, I would just like to deal with a layout that:
- is aware of the number of elements I want per row (maybe via configuration?),
- automatically stacks the TextViews horizontally, wrapping them on the next row only if they fill up the previous, so that I could simply cycle through the array elements and do
myLayout.add(TextView).
Do we have something like that, or there's no other better way than handcrafting it?
In the latter case, how would you've done that?
TableLayout tab_lay = (TableLayout) viewRef.findViewById(R.id.myTableLayout);
TableRow table_row = new TableRow(contextRef);
int col_counter = 0;
for (TextView aTextView : arrayOfTextViews) {
table_row.addView(aTextView);
col_counter++;
if (col_counter == 3) {
tab_lay.addView(table_row);
table_row = new TableRow(contextRef);
col_counter = 0;
}
}