LinearLayout is also a ViewGroup that is responsible for holding views in it and It can be a layout that arranges its children i.e the various views and layouts linearly Whether all these children can be arranged horizontally or vertically depends upon the value of attribute android:orientation and if you are not defining anything then By default the orientation is horizontal.(one after another) in a single column(vertical) or a single row(horizontal).
Vertical Linear Layout
In a vertical LinearLayout as its name suggests its the Views can be defined inside the LinearLayout which can be arranged vertically one after another and similar in a column and for this we need to mention the android:orientation parameter with value verticalAs we can see, there are 6 children inside the within LinearLayout tag.
To understand more clearly, let us check the following code and its output shown.
As we can see, there are six children inside the Linear Layout which all are TextView and Also the orientation parameter is set to vertical and all of the six children will appear one after another vertically.
In a horizontal LinearLayout as the name suggests it’s the Views which can be inside the LinearLayout which is arranged horizontally one after another, like in a row and it’s a good practice to explicitly specify the orientation of the linear layout by setting the attribute android:orientation with value horizontal in LinearLayout tag.
To understand clearly, let’s check the following code and it’s output shown.
As we can see that there are 3 children inside the LinearLayout and also the orientation attribute is set to horizontal, all of the 3 children.
LinearLayout within a LinearLayout
A LinearLayout also is a ViewGroup and can contain other layouts too. Let’s see the following code in which we have added one LinearLayout inside another LinearLayout, along with a few Views too.
The first LinearLayout has 3 TextViews and 1 LinearLayout as it’s children and it is set to vertical orientation, all of it’s children will appear vertically.
The second LinearLayout has 3 TextViews and it is set to horizontal orientation; also all it’s children will appear horizontally within this LinearLayout.