3

I'm programmatically putting various TextViews into a LinearLayout with a horizontal orientation.

After 2h of research I couldn't find out how to tell Android not to squeeze all the TextViews in one line but instead to "float" non-fitting TextViews into the next line.

I know there isn't something like actual "lines" in a LinearLayout, but how can I tell the TextViews to actually behave like floating DIVs from the HTML world?

Thanks alot!

Dennis Winter
  • 2,027
  • 4
  • 32
  • 45

2 Answers2

4

Use RelativeLayout. In addition to allowing to you to set up Views relative to each other, it can also align them relative the parent.

Specifically, look at RelativeLayout.LayoutParams, with which you can do something similar to float with alignParentRight/alignParentLeft and so on.

Charlie Collins
  • 8,806
  • 4
  • 32
  • 41
  • Thanks Charlie. But wouldn't that mean that I would have to measure each View and to programmatically align them relative to each other? I already tried a solution with a RelativeLayout, but without explicit positioning I couldn't get it to work. I was hoping to get around measuring... – Dennis Winter Jan 07 '11 at 16:39
  • 2
    If you have a known quantity of elements RelativeLayout works well, if you have many (and especially if they are dynamic), then you'd want something more like a FlowLayout, which unfortunately Android doesn't have (a minor bone of contention in and of itself:http://code.google.com/p/android/issues/detail?id=4543). As kcoppock notes though, there are several floating (sorry) around. Maybe though you can come up with another approach that doesn't try to stuff as many dynamic items on each line (not knowing the situation it's hard to guess, but maybe a different approach would work better?). – Charlie Collins Jan 07 '11 at 17:20
  • Each element will contain the name of a a text message's recipient. So the number of elements depends to 100% on the user. Thanks a lot for your efforts. I tried the class kcoppock referred to and at this point it works fine with that. :) – Dennis Winter Jan 07 '11 at 17:42
2

It sounds like you're looking for something like a FlowLayout in Java? I found an answer in this question that looks immensely helpful for what you're trying to do.

Community
  • 1
  • 1
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274