Professional Documents
Culture Documents
and Activities
Lab - 3
Linear Layout
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.
You can specify the layout
direction with the
android:orientation attribute.
Layout Weight
For example, if there are three text fields and two of them declare a weight of 1, while the other
is given no weight, the third text field without weight will not grow and will only occupy the area
required by its content. The other two will expand equally to fill the space remaining after all three
fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now
declared more important than both the others, so it gets half the total remaining space, while the
first two share the rest equally.
Steps
1. Create a new project selecting File > New > New Android Project.
2. Open the layout file from res > layout > layoutdemo.xml folder in the project explorer.
3. Change the layout according to the code given in the snippet.
<LinearLayout 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:paddingLeft="10dip" android:paddingRight="10dip"
android:paddingTop="5dip" android:paddingBottom="5dip"
android:orientation="vertical">
</LinearLayout>
4. Click the Graphical Layout tab to open the graphical layout of your android application.
5. Drag and drop one Plain Text Field one Password Text Field and one Button from the
form widgets on the android graphical layout.
6. Open the layoutdemo.xml and insert the code between <LinearLayout> nodes.
<LinearLayout 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:paddingLeft="10dip" android:paddingRight="10dip"
android:paddingTop="5dip" android:paddingBottom="5dip"
android:orientation="vertical">
<EditText android:id="@+id/userName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="User Name"
android:ems="10" >
<requestFocus />
</EditText>
<EditText android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />
<Button android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Login" />
</LinearLayout>
7. Right click on the created project folder in the project explorer and select Run As > Android
Application.
8. The layout of the created application will be as show in the figure.
Relative Layout
RelativeLayout is a view group that displays child views in relative positions. The position of each
view can be specified as relative
to sibling elements (such as to
the left-of or below another view)
or in positions relative to the
parent RelativeLayout area (such
as aligned to the bottom, left of
center).
A RelativeLayout is a very
powerful utility for designing a
user interface because it can
eliminate nested view groups
and keep your layout hierarchy
flat, which improves performance.
If you find yourself using several
nested LinearLayout groups, you may be able to replace them with a single RelativeLayout.
Positioning Views
RelativeLayout lets child views specify their position relative to the parent view or to each other
(specified by ID). So you can align two elements by right border, or make one below another,
centered in the screen, centered left, and so on. By default, all child views are drawn at the top -
left of the layout, so you must define the position of each view using the various layout properties
available from RelativeLayout.LayoutParams.
android:layout_alignParentTop
If "true", makes the top edge of this view match the top edge of the parent.
android:layout_centerVertical
android:layout_below
Positions the top edge of this view below the view specified with a resource ID.
android:layout_toRightOf
Positions the left edge of this view to the right of the view specified with a resource ID.
These are just a few examples. All layout attributes are documented at
RelativeLayout.LayoutParams.
The value for each layout property is either a boolean to enable a layout position relative to the
parent RelativeLayout or an ID that references another view in the layout against which the view
should be positioned.
In your XML layout, dependencies against other views in the layout can be declared in any order.
For example, you can declare that "view1" be positioned below "view2" even if "view2" is the last
view declared in the hierarchy. The example below demonstrates such a scenario.
Program Steps
1. Create a new project selecting > File > New > New Android Project.
2. Open the layout file from res > layout > relativelayoutdemo.xml folder in the project
explorer.
3. Change the layout according to the code given in the snippet.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="16dp"
android:paddingLeft="10dp"
tools:context=".Main" >
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Name"
android:ems="10" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Male" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_centerHorizontal="true"
android:text="Female" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_alignTop="@+id/checkBox2"
android:text="Button" />
</RelativeLayout>
4. Right click on the created project folder in the project explorer and select Run As >
Android Application.
5. The layout of the created application will be as show in the figure.
Task 1: (3 marks)
Develop an android app using some drag and drop input elements like buttons form fields
using the Linear Layout Vertical.
Task 2: (3 marks)
Develop an android app using some drag and drop input elements like buttons form fields
using the Linear Layout Horizontal.
Task 3: (4 marks)
Develop an android app using some drag and drop input elements like buttons form fields
using the Relative Layout.
Task 4: (5 marks)
Develop an android using a combination of input elements having all three types of layouts
Linear Vertical, Linear Horizontal and Relative in a single screen or activity file. Try to separate
using label or by using different colors as backgrounds.