1

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;
    }
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Redoman
  • 3,059
  • 3
  • 34
  • 62
  • possible duplicate of [GridLayout vs TableLayout](http://stackoverflow.com/questions/16300540/gridlayout-vs-tablelayout) – corsair992 Feb 09 '15 at 04:26
  • Just because that question is related to the concept of table layout and its children elements it does not mean it covers what I am specifically asking here, in fact it doesn't, both neither in the question itself and the answes. Hence I don't see how you could mark this a duplicate. – Redoman Feb 10 '15 at 09:22
  • These are the only table layouts provided by the platform, unless you count `GridView`, which is a scrollable adapter-based dynamic container similar to `ListView`. Therefore I linked to a comparison between them. As hinted at in the answer, `GridLayout` has more automatic management, although it doesn't allow pre-defining the number of elements per row or column. Feel free to explore the documentation related to them. – corsair992 Feb 10 '15 at 09:45
  • Well are you sure that there is no way to do what I am trying to do? You're not just linking another question, you're marking my question as a duplicate, only based on the assumption that a different question could imply an answer to mine just because it is a broader question. First this hasn't explicitly happened yet, second even if it does, that wouldn't automatically make my question into a duplicate. The rationale and context might make a difference and lead to very diverse answers and advised paths. – Redoman Feb 11 '15 at 11:12
  • There is no layout that can be pre-configured with variable number of columns or rows. This can be very easily investigated and confirmed by looking though the various layouts. My linked question _does_ address your second point though, in the "cells do not normally overlap in a GridLayout" quote from the documentation. Also, note that you're encouraged to do your own research before posting questions, and since you didn't even mention `GridLayout` it doesn't look like you did much of it. – corsair992 Feb 11 '15 at 16:05
  • I know about GridLayout but Android API is always changing and not always thoroughly documented and also I didn't mention it because I didn't want the possible answers to focus just on this or that Layout type only. Instead I wanted to keep it open and see what it could be possible to come up with. I've only mentioned TableLayout because that's what I used for building a quick functioning example which had the purpose of illustrating a typical layout situation, in order to be able to ask for information on a broader level. – Redoman Feb 12 '15 at 16:09

0 Answers0