Recently I had to bare with something similar to what you're asking. I finally decided using a TableView
, and construct the TableRow
s dynamically. That seemed nice, until I faced the problem of how to know when to add a new item to the existing TableRow
or add the row to the TableView
and start a new one.
A workaround (some may even call it a hack) that I particularly don't like but works is the following: Measure the width of your screen, using a code like this:
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
You'll be able to get the width of your screen within your params.width
parameter. I mutiplied it by 0.95
to not strictly fit the rows, mainly because it might work incorrectly.
So now, you'll need to have a loop to process all the items you want to add, let's suppose they're TextView
s. Keep the count of the width you've already added, and if the new item's width surpasses the width, add it to the next row.
totalwidth = 0;
for (TextView tv : myTVitems) {
if (totalwidth + tv.getWidth() < parameters.width * 0.95) {
// Add it to the existing row
totalwidth += tv.getWidth();
}
else {
// Add the `TableRow` to the `TableView` and start a new `TableRow`
totalwidth = 0;
}
}
I'm writing this from memory so it may not compile without any explicit cast, but that's the idea.