I have a variable-size array
of tags that I want to show inside a gridlayout
. The problem is that the tags vary in length and it looks kind of messy to put them inside a statically
defined grid even when some tags are much bigger than others.
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:
| *** ****** ****** ** ***** |
| ** ***** *** ********* *** |
| ********* ***** *** |
| ************** ******** |
| ****** ******** ******** |
| ***************** |
| ************** ***** ***** |
I think you guys get the point.
Right now, I got something like this but its not quite doing what I need.
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 < 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 && 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);
}
This looks more like :
| ***** **** ******** **** |
| **** ******** |
| ******* ****** |
So I am also trying to get the width of each TextView
so that I can calculate the spaces manually and add items accordingly but this is also failing as the dimensions are still 0
because they are not drawn. So it seems I will have to use the API accordingly to get the desired behaviour.
I am still new to this GridLayout
and the api is quite large so could you guys help me out?