Android Linear Layout In XML and Code

In a linear layout, as the name suggests, all the elements or Views (like TextView , button ,…) inside this layout are displayed in a linear fashion, either Horizontally or Vertically and this behavior is set in android: orientation which is an attribute of the node Linear Layout.

LinearLayout Horizontal Orientation

In case of horizontal placement of elements in linear layout set the property to attribute android:setOrientation="horizontal" inside Linear Layout node .

If you want to specify or equally space views inside LinearLayout use android:weightSum property .android:weightSum : defines the maximum weight sum, and is calculated as the sum of the layout_weight of all the children if not specified explicitly.

LinearLayout Horizontal WeightSum

Let's consider an example with a LinearLayout with horizontal orientation and 3 TextView inside it. Now we want these TextView always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 .

LinearLayout Vertical WeightSum

Let's consider an example with a LinearLayout with vertical orientation and 3 TextView inside it. Now we want these TextView always to take equal space. To acheive this, you can set the layout_weight of each ImageView to 1 and the weightSum will be calculated to be equal to 3 .