Here I am discussing 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:
-
Relative to the container.
-
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:
- android:layout_alignParentTop
- android:layout_alignParentLeft
- android:layout_centerVertical
- android:layout_centerHorizontal
- android:layout_centerInParent
- android:layout_alignParentRight
- 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:
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:
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:
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:
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:
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:
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"/>
Relative to other widgets: There are four properties that determine the position of the widget in relation to other widgets:
- android:layout_above
- android:layout_below
- android:layout_toRightOf
- 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>
Have a nice learning.....