title: How to add elements to a GridLayout dynamically with different column widths for every row based on element size?
body: <p>I have a variable-size <code>array</code> of tags that I want to show inside a <code>gridlayout</code>. The problem is that the tags vary in length and it looks kind of messy to put them inside a <code>statically</code> defined grid even when some tags are much bigger than others.</p>
  
  <p>So I would like to be able to put tag after tag until there is no more space left for a full tag and then go to the next row. Basically something like this:</p>
  
  <pre><code>| *** ****** ****** ** ***** |
  | ** ***** *** ********* *** |
  | ********* ***** ***        |
  | ************** ********    |
  | ****** ******** ********   |
  | *****************          |
  | ************** ***** ***** |
  </code></pre>
  
  <p>I think you guys get the point.</p>
  
  <p>Right now, I got something like this but its not quite doing what I need.</p>
  
  <pre><code>int total = tags.size();
  int column = 3;
  int row = total / column;
  suggestedTagsLayout.setColumnCount(column);
  suggestedTagsLayout.setRowCount(row + 1);
  
  for (int i = 0, c = 0, r = 0; i &lt; total; i++, c++) {
      if (c == column) {
          c = 0;
          r++;
      }
      TextView tag = createNewTag(tags.get(i));
      tag.setBackgroundColor(Color.BLUE);
      tag.setLayoutParams(new ViewGroup.LayoutParams(
              ViewGroup.LayoutParams.WRAP_CONTENT,
              ViewGroup.LayoutParams.WRAP_CONTENT));
  
      GridLayout.Spec rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
      GridLayout.Spec colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
      if (r == 0 &amp;&amp; c == 0) {
          logger.e("spec");
          colspan = GridLayout.spec(GridLayout.UNDEFINED, 1);
          rowSpan = GridLayout.spec(GridLayout.UNDEFINED, 1);
      }
  
      logger.d("\n\ntag = " + tag.getText().toString());
  
      int width = tag.getWidth();
      int height = tag.getHeight();
  
      logger.d("width = " + width);
      logger.d("height = " + height);
  
      width = tag.getMeasuredWidth();
      height = tag.getMeasuredHeight();
  
      logger.d("getMeasuredWidth = " + width);
      logger.d("getMeasuredHeight = " + height);
  
      GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
              rowSpan, colspan);
      suggestedTagsLayout.addView(tag, gridParam);
  }
  </code></pre>
  
  <p>This looks more like :</p>
  
  <pre><code>| ***** **** ******** **** |
  | ****       ********      |
  | *******    ******        |
  </code></pre>
  
  <p>So I am also trying to get the width of each <code>TextView</code> so that I can calculate the spaces manually and add items accordingly but this is also failing as the dimensions are still <code>0</code> because they are not drawn. So it seems I will have to use the API accordingly to get the desired behaviour.</p>
  
  <p>I am still new to this <code>GridLayout</code> and the api is quite large so could you guys help me out?</p>