Mindmajix

RelativeLayout & TableLayout in Android

RelativeLayout

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 or center).

Relative To Container

These properties will layout elements relative to the parent container.

  • android:layout_alignParentBottom – Places the bottom of the element on the bottom of the container
  • android:layout_alignParentLeft – Places the left of the element on the left side of the container
  • android:layout_alignParentRight – Places the right of the element on the right side of the container
  • android:layout_alignParentTop – Places the element at the top of the container
  • android:layout_centerHorizontal – Centers the element horizontally within its parent container
  • android:layout_centerInParent – Centers the element both horizontally and vertically within its container
  • android:layout_centerVertical – Centers the element vertically within its parent container

Here is a sample XML Layout

<RelativeLayout

android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button

android:id="@+id/backbutton"
android:text="Back"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />

<TextView

android:id="@+id/firstName"
android:text="First Name"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:layout_below="@id/backbutton" />

<EditText

android:id="@+id/editFirstName"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstName"
android:layout_below="@id/backbutton"/>

<EditText

android:id="@+id/editLastName"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editFirstName"
android:layout_alignLeft="@id/editFirstName"/>

<TextView

android:id="@+id/lastName" android:text="Last
Name" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/editLastName"
android:layout_below="@id/editFirstName" />

</RelativeLayout>

You probably noticed that we had to rearrange the elements in the XML since, you cannot reference an element that has not already been laid out. Here is what the updated RelativeLayout produces.

Screenshot_12

TableLayout

TableLayout is a ViewGroup that displays child View elements in rows and columns.

TableLayout organizes content into rows and columns. The rows are defined in the layout XML, and the columns are determined automatically by Android. This is done by creating at least one column for each element. So, for example, if you had a row with two elements and a row with five elements then you would have a layout with two rows and five columns.

table layout

Here is some sample XML using TableLayout.

<TableLayout

android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow>

             <Button

android:id="@+id/backbutton"
android:text="Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

<TableRow>

<TextView

android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText

android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

<TableRow>

<TextView

android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1" />

<EditText

android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</TableRow>

</TableLayout>

This is the result of that XML

Screenshot_13


 

0 Responses on RelativeLayout & TableLayout in Android"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.