What is android:weightSum in android, and how does it work?

Dhiral Pandya picture Dhiral Pandya · Sep 17, 2011 · Viewed 169.5k times · Source

I want to know: What is android:weightSum and layout weight, and how do they work?

Answer

Shubhayu picture Shubhayu · Oct 8, 2012

Adding on to superM's and Jeff's answer,

If there are 2 views in the LinearLayout, the first with a layout_weight of 1, the second with a layout_weight of 2 and no weightSum is specified, by default, the weightSum is calculated to be 3 (sum of the weights of the children) and the first view takes 1/3 of the space while the second takes 2/3.

However, if we were to specify the weightSum as 5, the first would take 1/5th of the space while the second would take 2/5th. So a total of 3/5th of the space would be occupied by the layout keeping the rest empty.