Mad Unit 3
Mad Unit 3
LinearLayout
The LinearLayout is the most basic layout, and it arranges its elements sequentially, either
horizontally or vertically. To arrange controls within a linear layout, the following attributes are used
android:orientation—Used for arranging the controls in the container in horizontal or vertical order
android: layout _width—Used for defining the width of a control
android:layout_height—Used for defining the height of a control
android:padding—Used for increasing the whitespace between the boundaries of the control and
its actual content.
android: Layout_weight—Used for shrinking or expanding the size of the control to consume the
extra space relative to the other controls in the container
android:gravity—Used for aligning content within a control
android:Layout_gravity—Used for aligning the control within the container
Applying the orientation Attribute
This attribute arranges its children in horizontal or vertical order. The valid values are horizontal
and vertical.
The orientation can be modified at runtime through the setOrientation() method. That is, by
supplying the values horizontal or vertical to the setOrientation() method.
Create a new android project called LinearlayoutApp. The default content of layout file
activity_linear_layout_app.xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world"
tools:context=".LinearLayoutAppActivity"/>
</LinearLayout>
Let's apply the LinearLayout and add three Button controls to the layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/Apple"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Apple" />
To see the controls horizontally, set the orientation to horizontal. The layout_width of three
controls is set to wrap_content, otherwise we will be able to see only the first control. Modify the content
of layout file activity_linear_layout_app.xml to
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple" />
<Button
android:id="@+id/Mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mango" />
<Button
android:id="@+id/Banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Banana" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content“
android:layout_weight=“0.0" />
<Button
android:id="@+id/Mango"
android:text="Mango
android:layout_width="wrap_content"
android:layout_height=wrap_content"
android:layout_weight="1.0" />
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="wrap_content"
android:layout_height=wrap_content"
android:layout_weight="0.0" />
</LinearLayout>
Similarly we set the weight of Apple, Mango, and Banana to 0.0, 1.0 and 0.5, respectively then
check the output.
Fig: The weight attribute of the Apple, Mango, and Banana Button controls set to 0.0, 1.0, and 0.5
android:gravity="center_horizontal|center_vertical”
The following figure shows the android:gravity attribute set lo left and right for the button controls
Mango and Banana.
Fig: The text in the Mango and Banana Button controls aligned to the left and right, respectively, through the
android:gravity attribute
Fig: (left) The three Button controls vertically aligned with the width attribute set to wrap_content, (middle)
the Mango and Banana Button controls aligned to the center and right of container, and (right) the width of
the three Button controls expanded to take up all the available space.
If we now set the values of three controls to match parent ,the button controls will expand
to take up the available space of the container.(right in above fig)
Now we can apply the android gravity attribute to align the text within the controls. Add
the following three attributes to align the content to the left, center and right within the control.(left)
android:gravity="left" android:gravity="center" and android:gravity="right“
For example, assigning the android: layout_ weight=“1.0" to all three controls results in equal
division of empty space among them.(middle)
Fig: (left) The three Button controls with their text aligned to the left, center, and right, (middle) the vertical
available space of the container apportioned equally among the three Button controls, and (right) the text of
the three Button controls vertically aligned to the center
In the middle image, the text in apple and banana controls is not vertical center. So, modify
their android:gravity value as,
android:gravity="center_vertical" for the Apple control
RelativeLayout
In RelativeLayout, each child element is laid out in relation to other child elements; that is, the
location of a child element is specified in terms of the desired distance from the existing children. Create
a new android project called RelativeLayoutApp. Modify its layout file activity_relative_layout_app.xml
to appear as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip"/>
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@+id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
android:layout_alignParentTop="true"/>
<Button
The attributes that control the alignment of a control in relation to other controls are
android: layout_alignTop— The top of the control is set to align with the top of the referenced
control.
android: layout_alignBottom—The bottom of the control is set to align with the bottom of the
referenced control.
android: layout_alignLeft—The left side of the control is set to align with the left side of the
referenced control.
android:layout_alignRight—The right side of the control is set to align with the right side of the
referenced control.
android: layout_alignBaseline—The baseline of the two controls will be aligned.
For spacing, Android defines two attributes android:layout_margin and
android:padding.The android:layout_margin attribute defines spacing for the container, while
android:padding defines the spacing for the view. Let's begin with padding.
android:padding—Defines the spacing of the content on all four sides of the control.
To define padding for each side individually, use
android:paddingTop—Defines the spacing between the content and the top of the control.
android:paddingBottom—Defines the spacing between the content and the bottom of the control.
android:paddingLeft—Defines the spacing between the content and the left side of the control.
android:paddingRight—Defines the spacing between the content and the right side of the control.
Here are the attributes that define the spacing between the control and the container:
android:layout_margin—Defines the spacing of the control in relation to the controls or the
container on all four sides. To define spacing for each side individually, we use the
android:layout_marginLeft, android:layout_marginRight, android:layout_marginTop, and
android:layout_raarginBottom options.
android:layout_marginTop—Defines the spacing between the top of the control and the related
control or container.
android:layout_marginBottom—Defines the spacing between the bottom of the control and the
related control or container.
android:layout_marginRight—Defines the spacing between the right side of the control and the
related control or container.
android:layout_marginLeft—Defines the spacing between the left side of the control and the
related control or container.
We don’t need to make any changes to the RelativeLayoutAppActivity.java file. Its original
content is
package com.androidunleashed.relativelayoutapp;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extendsActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative_layout_app);
}
}
When the application is run, we see the output as
Fig: (left) The Login Form displays an error if fields are left blank, (middle) the Password Incorrect message
displays if the user ID or password is incorrect, and (right) the Welcome message displays when the correct
user ID and password are entered.
AbsoluteLayout
Each child in an AbsoluteLayout is given a specific location within the bounds of the container.
Such fixed locations make AbsoluteLayout incompatible with devices of different screen size and
resolution. The controls in AbsoluteLayout are laid out by specifying their exact X and Y positions. The
coordinate 0,0 is the origin and is located at the top-left corner of the screen.
The AbsoluteLayout class is not used often, as it is not compatible with Android phones of
different screen sizes and resolutions.
Create a new Android Project called AbsoiuteLayoutApp. Modify its layout file, activity_
absolute_layout_app.xml, as
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="90dip"
android:layout_y="2dip"
android:text="New Product Form"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Code"
android:layout_x="5dip"
android:layout_y="40dip"/>
<EditText
android:id="@+id/p_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="110dip"
android:layout_y="30dip"
android:minWidth="100dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Product Name"
android:layout_x="5dip"
android:layout_y="90dip"/>
<EditText
android:id="@+id/p_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_x="110dip"
android:layout_y="80dip"
android:minWidth="200dip"
android:scrollHorizontally="true” />
Using ImageView
An imageview control is used to display images in Android applications. An image can be
displayed by assigning it to the imageview control and including the android: src attribute in the XML
definition of the control. Images can also be dynamically assigned to the ImageView control through
Java code.
A sample ImageView tag when used in the layout file is shown here:
<lmageView
android:id=“@+id/first_image"
android:src = "@drawable/bintupic"
android:layout_width="wrap_content"
android: layout_height= "wrap_content11
android: scaleType="fitXY"
android: adjustViewBounds=11 true"
android: maxHeight=“100dip"
android:maxWidth=“25Odip"
android:minHeight="100dip"
android: minWidth=“25Odip"
android:resizeMode="horizontal|vertical" />
android:src—Used to assign the image from drawable resources. You do not need to specify the
image file extension. JPG and GIF files are supported, but the preferred image format is PNG.
android:scaleType—Used to scale an image to fit its container. The valid values for this attribute
include fitXY, center, centerlnside, and fitCenter. The value fitXY independently scales the image
around the X and Y axes without maintaining the aspect ratio to match the size of container.
Android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the imageview
control to maintain the aspect ratio of the image displayed through it.
android:resizeMode—The resizeMode attribute is used to make a control resizable so we can
resize it horizontally, vertically, or around both axes. The available values for the resizeMode
attribute include horizontal, vertical, and none.
FrameLayout
FrameLayout is used to display a single view. The view added to a FrameLayout is placed at the
top-left edge of the layout. Any other view added to the FrameLayout overlaps the previous view; that is,
each view stacks on top of the previous one.
To display images in Android applications, the image is first copied into the res/drawable folder
and from there, it is referred to in the layout and other XML files.
There are four types of drawable folders. We have to place images of different resolutions and
sizes in these folders. The graphics with the resolutions 320dpi, 240dpi, 160 dpi, and 120dpi (96 X 96
px, 72 x 72 px, 48 X 48 px, and 36 X 36 px), are stored in the res/drawable-xhdpi, res/drawable-hdpi,
<ImageView
android:id="@+id/first_image"
android:src="@drawable/ex_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<ImageView
android:id="@+id/second_image"
android:src="@drawable/ex_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the image to switch"
android:layout_gravity="center_horizontal|bottom"
android:padding="5dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#333333"
android:layout_marginBottom="10dip" />
</FrameLayout>
When a user selects the current image on the screen, the image should switch to show the hidden
image. For this to occur, we need to write code in the activity file as
Package com.androidunleashed.framelayoutapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View.OnClickListener;
import android.view.View;
Fig: (left) An image and a Textview laid out in FrameLayout, and (right) the images switch when clicked
TableLayout
The TableLayout is used for arranging the enclosed controls into rows and columns. Each new row
in the TableLayout is defined through a TabieRow object. A row can have zero or more controls, where
each control is called a ceil. The number of columns in a TableLayout is determined by the maximum
number of cells in any row. The width of a column is equal to the widest cell in that column.
Operations Applicable to TableLayout
We can perform several operations on TableLayout columns, including stretching, shrinking,
collapsing, and spanning columns.
Stretching Columns: The default width of a column is set equal to the width of the widest column,
but we can stretch the column(s) to take up available free space using the android: stretchcolumns
Examples:
1. android: stretchcolumns=“1"—The second column (because the column numbers are zero-
based) is stretched to take up any available space in the row.
Let's try arranging controls in a TableLayout with an example. Create a new Android project called
TableLayoutApp. Make its layout file activity_table_layout_app.xml appear as
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width= "match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Product Code:"
android:layout_column="0"/>
<EditText android:id= "@+id/prod_code"
android:layout_height="wrap_content"
<TableRow>
<TextView android:layout_height= "wrap_content"
android:text="Product Name:"
android:layout_column="0"/>
<EditText android:id="@+id/prod_name"
android:layout_height = "wrap_content"
android:scrollHorizontally= "true" />
</TableRow>
<TableRow>
<TextView android:layout_height="wrap_content"
android:text="Product Price:" />
<EditText android:id="@+id/prod_price"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<Button android:id="@+id/add_button"
android:text="Add Product"
android:layout_height="wrap_content" />
<Button android:id="@+id/cancel_button"
android:text= "Cancel"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
We cannot specify the layout width attribute for the controls enclosed within the TableLayout, as
their width will be always set to match_parent by default. We can specify the layout height attribute for
the enclosed controls. The layout_height attribute of the TableRow is always wrap_content.
If we don't specify a column number for any cell, it is considered to be the next available column.
When the application is run, the controls are laid out in rows and columns, as
GridLayout
GridLayout lays out views in a two-dimensional grid pattern, that is, in a series of rows and
columns. The intersection of row and column is known as a grid cell, and it is the place where child
views are placed. It is easier to use GridLayout when compared to TableLayout. Without specifying
intermediate views, we can flexibly place the views randomly in the grid by specifying their row and
column positions. More than one view can be placed in a grid cell.
Specifying Row and Column Position:
The two attributes that are used to specify the row and column position of the grid cell for inserting
views are android: layout_row and android: layout_column. Together, they specify the exact location of
the grid cell for placing the view. For example, the following statements place the view at the first row
and column position of the grid:
android: layout_row=”0” android: layout_column=”0“
Spanning Rows and Columns:
Views can span rows or columns if desired. The attributes are android: layout_rowSpan and
android: layout_columnSpan. For example, statement spans the view to two rows:
android:1ayout_rowSpan=”2”
Similarly, the following statement spans the view to three columns:
android: layout_columnSpan=”3”
Inserting Spaces in the GridLayout:
To insert spaces, the Space view is inserted as a child view. For example, the following statement
insert a space at the second row in the GridLayout.
let's create a new Android project called GridLayoutLayoutApp. The application has controls
arranged in the same way as we saw in TableLayout. Make its layout file, activity_grid_layout_app.xml,
appear as
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width= "match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:rowCount="7"
android:columnCount="2" >
<TextView
android:layout_row="0"
android:layout_column="0"
android:text="New Product Form"
android:typeface="serif"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal"
android:textSize="20dip" />
<Space
android:layout_row="1"
android:layout_column="0"
android:layout_width="50dp"
android:layout_height="10dp" />
<TextView
android:layout_row="2"
android:layout_column="0"
android:text="Product Code:" />
<EditText
android:id="@+id/prod_code"
android:layout_width="100dip" />
<TextView
android:text="Product Name:" />
<EditText
android:layout_row="3"
android:layout_column= "1"
android:id="@+id/prod_name"
android:layout_width="200dip" />
<TextView
android:layout_row="4"
android:layout_column="0"
In the preceding code, the GridLayout is defined as consisting of seven rows and two columns.
When the application is run, the controls are laid out in the grid pattern as
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dip"
android:layout_marginLeft="20dip"/>
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="28dip"
android:layout_toRightOf="@+id/Apple"
android:layout_marginLeft="15dip"
android:layout_marginRight="10dip"
android:layout_alignParentTop="true"/>
<Button
android:id="@+id/Banana"
android:text="Banana"
android:layout_width="200dip"
android:layout_height="50dip"
android:layout_marginTop="15dip"
android:layout_below="@id/Apple"
android:layout_alignParentLeft="true"/>
<Button
android:id="@+id/Grapes"
android:text="Grapes"
android:layout_width="wrap_content"
android:layout_height="match_parent"
import android.app.Activity;
import android.os.Bundle;
public class ScreenOrientationAppActivity extends Activity {
@Override
public void onCreate(Bundle savedlnstanceState) {
super.onCreate (savedlnstanceState);
setContentView(R.layout.activity_screen_orientation_app);
}
}
When the application is run while in the default portrait mode, the controls appear as shown in Fig
(left). Because the five Button controls are placed in relation to the four edges of the container and in
relation to each other, none of the Button controls disappear if the screen is rotated to landscape mode, as
shown in Fig (right).
Fig: (left) Controls in portrait mode, and (right) the controls in landscape mode.
Defining Layout for Each Mode
Fig: (left) Controls in portrait mode, and (right) some controls disappear in landscape mode.
To use the blank space on the right side of the screen in landscape mode, we need to define another
layout file, activity_screen_orientation_app.xml, created in the res/ layout-land folder. The layout-land
folder has to be created manually inside the res folder. Right-click on the res folder in the Package
Explorer window and select the New, Folder option. A dialog box opens, asking for the name for the
new folder. Assign the name layout-land to the new folder, and click the Finish button.
Copy the activity_screen_orientation_app .xml file from the res/layout folder and paste it into
res/layout-land folder. Modify the activity_screen_orientation_app.xml file in the res/layout-land folder
so as to arrange the controls in landscape mode.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height= "match_parent" >
<Button
android:id="@+id/Apple"
android:text="Apple"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip" />
<Button
android:id="@+id/Mango"
android:text="Mango"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:padding="20dip"
android:layout_marginTop="20dip"
android:layout_toRightOf="@id/Apple" />
<Button
android:id="@+id/Banana"
android:text="Banana"
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
Fig: (left) Controls in portrait mode, and (right) all controls are visible in landscape mode.