As we said before, let's start talking about the widgets, containers or layouts.
There are lots of kinds of layouts, with different characteristics and properties. Let's see the most important ones.
In LinearLayouts we can add children in one direction. We can add them horizontally or vertically (depending on the property we have set on the LinearLayout). The next images will show you more clearly what I was talking about.
Here we see four buttons inside a LinearLayout set vertically.
Here we can see the same four buttons but now horizontally.
This is the XML code for a LinearLayout.
Inside the tags <LinearLayout> and </LinearLayout> is the place where we put the widgets. As we see, this code has some properties. Let's have a look at them:
android:layout_width → Defines the width of the Layout. The values it can hold are:
fill_parent → The layout fill al the parent width, if there is no parent, it will fill all the screen.
wrap_content →Depending on the content of the layout, the width will be fixed to it.
Xpx → Where X is a number. You can determine the width of our layout in pixels.
android:layout_height → The same as the “layout_width", but considering the Height of the layout.
android:id → This string is considered the identifier of the layout. When we have more than one layout or widget, it's interesting to have them identified.
android:orientation → this is the parameter we were talking about before. Here we can change the orientation of the layout: horizontal or vertical.
All layouts that are we going to see next are similar to the LinearLayout. They only change in the way of displaying the widgets.
With this layout, we can put the widgets wherever we want, just with the coordinates. Considering that the point (0,0) is in the top-left.
In the RelativeLayout, we set the place of the widgets in relation to others widgets or the parent. Here it is important the “android:id" parameter.
For example. If we have a button in the middle of the screen, we can add another widget 10px to the right of this initial button.
We can set the position of the widgets in rows and columns. The TableLayout holds TableRows elements. Each of these TableRows can hold one or more columns.