RelativeLayout in Android Using Visual Studio 2010


Here I am discussiing the second type of view; for the first see my previous article.

RelativeLayout

The RelativeLayout organizes layout components in relation to each other. This provides more flexibility of component positioning than LinearLayouts. The position of a view can be specified relative to sibling elements or in positions relative to the RelativeLayout area; while working with this layout we need to have a very clear idea of how the elements of the screen are going to be displayed.

The drawback of this layout is that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

The relative layout widgets can be placed in one of two ways; these are:

  1. Relative to the container.

  2. Relative to other widgets.

Relative to the container: When we talk about container relative layout it means the widgets are placed in positions relative to their container. We can place the widgets in seven different positions by using these following seven properties:

  1. android:layout_alignParentTop

  2. android:layout_alignParentLeft

  3. android:layout_centerVertical

  4. android:layout_centerHorizontal

  5. android:layout_centerInParent

  6. android:layout_alignParentRight

  7. android:layout_alignParentBottom

Let's see the example one-by-one:

android:layout_alignParentTop

<TextView
     
android:layout_alignParentTop="true"
     
android:text=    "layout align Top"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout alignParentTop

android:layout_alignParentLeft

<TextView
     
android:layout_alignParentLeft="true"
     
android:text=    "layout align Left"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentLeft

android:layout_centerVertical

<TextView
     
android:layout_centerVertical="true"
     
android:text=    "layout center Vertical"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerVertical


android:layout_centerHorizontal

<TextView
     
android:layout_centerHorizontal="true"
     
android:text=    "layout center Horizontal"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerHorizontal

android:layout_centerInParent

<
TextView
     
android:layout_centerInParent="true"
     
android:text=    "layout aligh center In Parent"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align centerInParent

android:layout_alignParentRight

<TextView      android:layout_alignParentRight="true"
     
android:text=    "layout align Right"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentRight

android:layout_alignParentBottom

<TextView
     
android:layout_alignParentBottom="true"
     
android:text=    "layout align Bottom"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>

Output:

layout align ParentBottom

Relative to other widgets: There are four properties that determine the position of the widget in relation to other widgets:

  1. android:layout_above

  2. android:layout_below

  3. android:layout_toRightOf

  4. android:layout_toLeftOf

Example:

<?xml version="1.0" encoding="utf-8"?>
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="fill_parent"
   
android:orientation="vertical"
   
android:layout_height="fill_parent">
  <
TextView
     
android:id="@+id/text1"
     
android:text=    "First"
     
android:textSize="30px"
     
android:background=    "#c0c0c0ff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"/>
  <
TextView
     
android:id="@+id/text2"
     
android:text=    "second"
     
android:textSize="30px"
     
android:background=    "#ffffffff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"
    
android:layout_below="@id/text1"/>
  <
TextView
     
android:id="@+id/text3"
     
android:text=    "Third"
     
android:textSize="30px"
     
android:background=    "#ff0fffff"
     
android:layout_width=    "wrap_content"
     
android:layout_height=    "wrap_content"
     
android:layout_toRightOf="@id/text2"/>
</
RelativeLayout>

Output:

Position align

Have a nice learning.....

Next Recommended Readings