views:

52

answers:

2

Ok, my problem is, I have got n-TextViews and they will be added programmatically into a TableLayout. It totally doesnt matter, how I style these TextViews or the TableLayout, everytime I add something, it adds the TextView on the bottom after the other TextViews. These Views have got a variable width, which is calculated out of their textlength and some pixels (WRAP_CONTENT just made 100% width...).

It is like this right now:
------------------------------------------
[TextView 1]
[TextView 2]
[TextView 3]
[TextView 4]
------------------------------------------

And it should be like:
------------------------------------------
[TextView 1] [TextView 2] [TextView 3]
[TextView 4]
------------------------------------------

Actually I dont need a TableLayout. I still can change it in whatever you want. Of course it would be even better if those TextViews can get WRAP_CONTENT as width.

EDIT: Btw. a LinearLayout with orientation="horizontal" adds the TextViews at first corret, but on the end it doesn't wrap to the next line, it just adds them to the right side and it will be splitted, like:

------------------------------------------
[TextView 1] [TextView 2] [TextView 3] [Te
                                       [xt
                                       Vie
                                       w 4
                                       ]
------------------------------------------
A: 

You might want to use a GridView for what you are trying to do. I do not believe there are any Layout classes that support what you want to do.

If you go with GridView you should then be able to use the ArrayAdapter since it already supports creating TextViews easily.

If you just want a Layout that wraps around if the next child does not fit in the current row then you're going to have to write a custom layout class to do it.

Qberticus
Well, a GridView has got the disadvantage, that the single childs are not dynamic with their width. My TextViews are sometimes small, sometimes as width as the screen.
Keenora Fluffball
A: 

I think you could try something like that :
- Put a TableLayout as root of your Activity, with FILL_PARENT as width and height.
- Then get its width and store it in a variable (mWidth).
- you create a TableRow, add a first TextView and store its width in a variable (mTotalWidth), and you put the TextView in the row.
- When you want to add a new Textview, you create it, get its width and calculate the sum when you add it to mTotalWidth.
Now
if mTotalWidth < mWidth , you add your TextView into the current row and increment mTotalWidth
else you create a new TableRow, put your Textview in it, and change the value of mTotalWidth to the width of this TextView.
Then you can repeat this for all your views. This can appear a bit messy but I think it could work.

Sephy
This sounds like a good idea! Its a bit crazy but well :)Only disadvantage in my case, I built it like this: When you clicked on some rows in a ListView it adds TextViews to the top. When I click the TextViews itself, they'll be removed from the top.The removing would be a problem with this solution...
Keenora Fluffball
Actually, i don't see any issue. There is a removeView method for `TableLayout` so where is the problem? http://developer.android.com/reference/android/view/ViewGroup.html#removeView%28android.view.View%29
Sephy
I would have to check the length again and if there is a TextView in the row still. Like: Is there a TextView in the TableRow, then do not remove the TableRow, if there is no TextView in the TableRow, remove the TableRow. If there are 3 TextViews in the second TableRow of 5 Rows, and I delete one of those, what about the rest? ;)
Keenora Fluffball
Well yeah I agree, the management of the layout would be quite complicated
Sephy