Hello everyone.
Now that we know how to install Android on our machine, and how to create a simple application, we will learn to create layouts in Android. In Android, layouts are created using XML files with a very rigid structured and standard language, but also very simple and intuitive, due to the excellent structure created by Google.
There are several types of layout in Android:
FrameLayout,
LinearLayout,
TableLayout, among others. In this post, we'll cover basically the 3 layouts cited in this paragraph.
FrameLayout
The FrameLayout is the most basic layout on Android. In this layout, we have only one layout structure stored. However, such structure may be another layout. This layout is often used in generic layouts, such as tabs and screens whose layout changes throughout it's execution. We will use this layout as a basis to teach many basic concepts of layout in Android.
For example, in Android there are various types of graphic components:
- TextView → Displays text on the screen. It's definitely the most used graphic component in Android.
- ImageView → Shows a picture or just a window on the screen. Every time you need to show a picture or a colored window, this component is used.
- EditText → The above components only shows informations on the screen. But EditText gets a text typed by the user, which can be used to interact with the Android application.
- Button → This component is one of the most common in any layout system. In this component, an action is performed after a click be given.
- CheckBox → A component that basically has two values: true or false. Commonly used to represent the system settings.
- RadioGroup → Quite useful component to show several options listed on the screen, where only one of them must be clicked. Widely used in multiple-choice questions.
And there are several parameters for each component in a graph layout in Android. To fully understand how those parameters, below is shown the use of some of these components in a
FrameLayout. Below is shown the source code that creates the XML layout.
And the generated layout.
In the source code above, we have some parameters, which are explained below:
- android:layout_width="match_parent" → In creation of layouts in Android, we always call a parameter using the prefix "android:". In this case, the last word of the parameter is "layout_width". This parameter defines the width size of the screen we are creating. This size can be a fixed size (in pixels, pixel density, or other formatting units) or expandable sizes. In this case, it hass a expandable size. There are 3 expandable sizes in Android:
- fill_parent → With this option, the parameter size is maximum (ie, the current screen size). For example, if the mobile phone screen or tablet that we're dealing is 800 x 600 pixels, if we have in a FrameLayout the parameter android:layout_width="fill_parent", the width size of the screen we are creating will be 600 pixels. Starting from API 8 (Froyo, Android 2.2), this option was discontinued, and it's operation became the "match_parent" option.
- wrap_content → With this option, the parameter size will be minimal, based on the child components of the current layout. For example, if we define the width of the current layout with layout_width parameter, as follows: android:layout_width="wrap_content", we are indicating that the width size of the screen is the size of the child component of this layout. In the case of the XML we are analyzing, it's child component is a TextView. If, for example, this TextView has a size of 20 x 20 (height x width) pixels, then the size of the layout is the same. That's what makes wrap_content. If we have fill_parent instead, the layout size wouldn't be 20 x 20 pixels, but 800 x 600 pixels (equivalent to the size of the mobile phone screen).
- match_parent → Keeps the size inherited by the parent component. If there is no parent component, the size will be maximum (ie, the screen size). In XML we have a TextView and its parent component, which is a FrameLayout. If we put in the TextView parameter android:layout_width="match_parent", the width of the current screen will be the same width as the FrameLayout.
- android:layout_height="match_parent" → This parameter defines the height of the screen that we are creating.
- android:gravity="center_horizontal" → This parameter sets the alignment of the components of this layout. In this case, the alignment will be centered and horizontal.
- android:text="Example" → This parameter defines the text of the current component.
Well, many concepts have been explained above. To understand them better, there is no other way than pick the options of each parameter and test. For example, here we have the same XML from above, but this time with the parameter
android:gravity component from the changed
TextView, from
"center_horizontal" to
"center_vertical".
And below we have the same XML as above, but this time with the parameter
android:gravity from the
TextView component, changed from
"center_horizontal" to
"center".
Let's continue modifying components options, because this way we will understand better the behavior of each parameter. Below there is the resulting layout when we modify the parameters
android:layout_width and
android:layout_height to
"match_parent" to
"wrap_content".
Thus, the size of the
FrameLayout became the same size as the
TextView. Hard to understand? So let's give the
TextView a color:
The result is shown below.
Now it's easy to understand that the
FrameLayout size of became the same size of the
TextView. Now, if we change the parameters
android:layout_width and android:layout_height of the
FrameLayout from
"wrap_content" to
"match_parent" (which is the original value, displayed in the 1st picture of this topic), we have the following result.
So, in the previous example, the size of
FrameLayout had become the same size of the
TextView, through the use of
"wrap_content". Now, the size of the
FrameLayout became the size of the mobile phone screen, by using the
"match_parent" option. Understanding these concepts of screen size expandable options is
INDISPENSABLE to develop any layout in Android. It's extremely important to understand these concepts, otherwise the creation of layout's in Android will become much more complicated.
Continuing
FrameLayout explanation, is shown below the XML from above with some more options in order to make the text appear more elegant.
The result of this new XML is shown below:
In this new XML, we changed the background color of the FrameLayout from black (default color) to blue, through the parameter
android:background with the value
"#2244AA", that is a soft blue. But why the majority of the screen is in white color? Because that's the color of the
TextView (through
option android:background="#FFFFFF"). As the size screen parameters from the
FrameLayout and
TextView are
"match_parent", the screen size is the size of the
TextView. However, we have a new parameter in
FrameLayout:
android:padding="5dp". What is this parameter? With this parameter, you create an inner margin between the
FrameLayout and the
TextView, with size of 5dp. The term "dp" can be understood here.
Thus, as the color of the
FrameLayout is blue, and from the
TextView is white, we have a contrast on the screen, giving the impression that a border was created.
Following the explanation, we have the options
android:textsize (text size),
android:textstyle (text style, which is bold in this case), and
android:textColor (text color, which is red in this case).
Now we will talk about other types of layout.
LinearLayout
In
FrameLayout we have only one child component. But in
LinearLayout, we have several. This layout is designed for cases like several structures on the screen in a linear way. In this layout we have a parameter that don't exist in
FrameLayout:
android:orientation. It can have
"vertical" or
"horizontal" options. To demonstrate it's operation, we will use another XML, shown below.
Now we have more than one component on the screen. The result of the above XML is shown below.
So, we have 2
TextView's, one under the other, in vertical orientation. Since both
TextView has the parameters
android:layout_width="match_parent" and
android:layout_height="wrap_content", we have the width with a maximum length and the height with minimum size (the size of the
TextView). Below is the inversion of those values, where the orientation of the
LinearLayout is horizontal.
Here is the result of this new XML:
As the
TextView's were very close, we can give a spacing between them. This is shown in the following XML:
Here is the result:
What was added was now the parameter
android:layout_margin="5dp". It created an outside edge (do not confuse with padding, which is an inset) between the 2
TextView's. But still, the texts are too "stuck" in
TextView. So now, we will use the padding to give an internal spacing in their writing.
Here is the result:
To end this basic explanation about
LinearLayout's, we will speak about the parameter
layout_weight. Below we have a new XML.
In it, we have included a new
TextView, and modified the orientation of the
LinearLayout from horizontal to vertical. However, we included the parameter
android:layout_weight="1". This parameter gives priority to the size of the
TextView where it is set (in this case, the 1st
TextView). In other words, it boosts the size of the width/height of this component, overwriting the effectiveness (making useful operation) of the parameter
android:layout_width and
android:layout_height. Thus, it "extends" the size of this
TextView to where the screen size leave. The result can be seen below:
And now, the result when we set
android:layout_weight="1" also in the 2nd
TextView.
And also in 3rd.
So, the parameter
layout_weight gives "weight" for each component. As in the previous image all components have same weight, they occupy the same place on the screen. However, this parameter overrides the functionality of parameters
android:layout_width and
android:layout_height. To finish
layout_weight explanation, what happens when we set all
TextView as
android:layout_weight="1", but the 1st
TextView as
android:layout_weight="2"?
That is what happends. It balanced the weight of components according to the value of the parameters
android:layout_weight of each
TextView.
Now, we will learn a bit about
TableLayout.
TableLayout
This layout is commonly used when we need listing various components in the same row, along a same screen. For example, create a layout with 18
TextView's split 3-3 along 6 lines. To create a layout like that, we could use 6
LinearLayout's, but it would spend much source code in XML, plus lots of memory on Android. The solution for this case would be using a
TableLayout.
If we wanna have certain components used in only one line of
TableLayout, the component used is
TableRow. Below is an example of it's use:
And the result of this XML:
With the above example, we can deduce what effect one
TableLayout has. If the component
TableRow wouldn't be used, the
TableLayout would become a
LinearLayout.
To better understand the functioning of
TableLayout, we will practice an exercise. We will create a layout with 6 lines, each containing 3
TextView's of equal size. The result will be the layout below.
Well, we can now realize that the generated XML source code will be huge (passing 250 lines for both cases), because we have to replicate enough code to create those 18's
TextView. One tip to avoid this re-typing job is to create a style for our
TextView. To create a style, we must create an XML file in the folder
res/values, called
styles.xml. In this file, we will create the generic layout of our
TextView's. Below is shown how would be the syntax of this file.
Now we need to put this new style in our XML. So, we would have as result an XML file with only 110 lines, more than half the size of lines of XML without using styles.
-----
With these 3 types of layout we can create a tremendous amount of layout in Android. There are other types of layout (and other visual components) that will be seen on the blog in the next posts.
If someone has a question, suggestion or criticism, feel at ease.