2

I have tried RelativeLayout and LinearLayout, havnt figured out on any of them.

I have a number of views(EditText) 5-10, some of them are enabled, some arn't. What i want is to have them all displayed. First should be placed topleft, then they should propagate to the right the first, and then on next line if filled up.

Is there any way to do this, without writing my entire Layout myself? :-)

Anders Metnik
  • 6,096
  • 7
  • 40
  • 79

3 Answers3

2

you can make layout like this row.xml

<LinearLayout....>

    <EditText1....>
    <EditText2....>

</LinearLayout>
-------------------------------INFLATE ROW TO YOUR LAYOUT
LinearLayout featureLayout = (LinearLayout) View.inflate(BonusFilms.this, R.layout.row, null);

yourLayout.addView(featureLayout); // u can use this in for loop
MAC
  • 15,799
  • 8
  • 54
  • 95
2

The property you are looking for is layout_weight, which would get set on the EditText elements within the LinearLayout.

Layout weight tells the UI how the elements within that container should allocate the space. More specifically, it tells the UI which how much space each element is entitled to. If you ever worked with html, it's like specifying percentages for your columns.

Say you have 2 EditText's and you want each of them to space the linear layout evenly, you would do this:

<LinearLayout...>
    <EditText
        android:layout_width="0dip"
        android:layout_weight="1">
    <EditText
        android:layout_width="0dip"
        android:layout_weight="1">
</Linearlayout>

Note: I'm not 100% sure why, but you need to specify 0dip for the property (in this case: Width) that you want to be distributed by the weight.

In the case above, each textbox is entitled to 50% of the space available in the Linear Layout. Why? Because the total weight of all the elements inside the LinearLayout is 2 and each textbox is entitled to 1 (See Formula Below)

([allocation for current element] / [sum of all the weights]) * 100 = % of space allocated

(1 / 2) * 100 = 50%

If you want the first textbox to take up 66% and the second textbox to take up 33%, you would do this:

<LinearLayout...>
    <EditText
        android:layout_width="0dip"
        android:layout_weight="2">
    <EditText
        android:layout_width="0dip"
        android:layout_weight="1">
</Linearlayout>

So that's how layout weight would help you, however, I don't think it has the ability to wrap around for two reasons. 1) The textboxes don't have any limit for how big or small they can be, so how would they know when they've reached the limit and need to wrap? You might be able to specify minimumWidth to force it to wrap at some point.

2) I don't think LinearLayout's wrap. They are one row that goes on endlessly. Seems like you might need to write your own custom layout class that involves performing measurements on what's contained within the class and how much screen space is available.

Android - LinearLayout Horizontal with wrapping children

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14
1

While i have had some really nice responses from guys, it just didn't really answer my question. The best solution, besides from writing my own heavy math layout is this: Sherif's answer

Community
  • 1
  • 1
Anders Metnik
  • 6,096
  • 7
  • 40
  • 79