CodeseniorSoftware Tutorials

Android How Much Of The Extra Space In The Layout To Be Allocated To The View

07-09-2014
LinearLayout supports assigning a weight to individual children. This attribute assigns an importance value to a view, and allows it to expand to fill any remaining space in the parent view. Default weight is zero

Calculation to assign any remaining space between child

space assign to child = (child individual weight) / (sum of weight of every child in Linear Layout)

Example (1): if there are three text boxes and two of them declare a weight of 1 , while the third one is given no weight (0) , then remaining space assign to

1st text box = 1/(1+1+0)
2nd text box = 1/(1+1+0)
3rd text box = 0/(1+1+0)

Example (2): let's say we have a text label and two text edit elements in a horizontal row. The label has no layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1 , the remaining width in the parent layout will be split equally between them (because we claim they are equally important).

Calculation:
1st label = 0/(0+1+1)
2nd text box = 1/(0+1+1)
3rd text box = 1/(0+1+1)

If the first one text box has a layout_weight of 1 and the second text box has a layout_weight of 2 , then one third of the remaining space will be given to the first, and two thirds to the second (because we claim the second one is more important).

Calculation:
1st label = 0/(0+1+2)
2nd text box = 1/(0+1+2)
3rd text box = 2/(0+1+2)