RelativeLayout in Android

The RelativeLayout is the most powerful layout available. All children in a RelativeLayout can be positioned relative to each other or to their parent.

For example :

you can tell a view to be positioned to the left or right of another view. Or, you can specify that a view is aligned to the bottom or top edge of its parent.

Positioning a child in a RelativeLayout is achieved using the below attributes

Attribute Description
layout_above Places the bottom edge of this view above the specified view ID.
layout_alignBaseline Places the baseline of this view on the baseline of the
specified view ID.
layout_alignBottom Aligns the bottom of this view with the specified view.
layout_alignEnd Aligns the end edge of this view with the end edge of
the specified view.
layout_alignLeft Aligns the left edge of this view with the left edge of
the specified view.
layout_alignParentBottom A value of true aligns the bottom of this view with the
bottom edge of its parent.
layout_alignParentEnd A value of true aligns the end edge of this view with
the end edge of its parent.
layout_alignParentLeft A value of true aligns the left edge of this view with
the left edge of its parent.
layout_alignParentRight A value of true aligns the right edge of this view with
the right edge of its parent.
layout_alignParentStart A value of true aligns the start edge of this view with
the start edge of its parent.
layout_alignParentTop A value of true aligns the top edge of this view with
the top edge of its parent.
layout_alignRight Aligns the right edge of this view with the right edge
of the given view.
layout_alignStart Aligns the start edge of this view with the start edge of
the given view.
layout_alignTop Aligns the top edge of this view with the top edge of
the given view.
ayout_alignWithParentIfMissing A value of true sets the parent as the anchor when the
anchor cannot be found for layout_toLeftOf,
layout_toRightOf, etc.
layout_below Places the top edge of this view below the given view.
layout_centerHorizontal A value of true centers this view horizontally within
its parent.
layout_centerInParent A value of true centers this view horizontally and
vertically within its parent.
layout_centerVertical A value of true center this view vertically within its
parent.
layout_toEndOf Places the start edge of this view to the end of the
given view.
layout_toLeftOf Places the right edge of this view to the left of the
given view.
layout_toRightOf Places the left edge of this view to the right of the
given view.
layout_toStartOf Places the end edge of this view to the start of the
given view.

 

Attributes for children of a RelativeLayout

As an example, the layout in Listing 4.4 specifies the positions of three views and a

RelativeLayout.

<RelativeLayout

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=“2dp”

android:paddingRight=“2dp”>

<Button

android:id=”@+id/cancelButton”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Cancel” />

<Button

android:id=”@+id/saveButton”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_toRightOf=”@id/cancelButton”

android:text=“Save” />

<ImageView

android:layout_width=“150dp”

android:layout_height=“150dp”

android:layout_marginTop=“230dp”

android:padding=“4dp”

android:layout_below=”@id/cancelButton”

android:layout_centerHorizontal=“true”

android:src=”@android:drawable/ic_btn_speak_now” />

<LinearLayout

android:id=”@+id/filter_button_container”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:layout_alignParentBottom=“true”

android:gravity=“center|bottom”

android:background=”@android:color/white”

android:orientation=“horizontal” >

<Button

android:id=”@+id/filterButton”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“Filter” />

<Button

android:id=”@+id/shareButton”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“Share” />

<Button

android:id=”@+id/deleteButton”

android:layout_width=“wrap_content”

android:layout_height=“fill_parent”

android:text=“Delete” />

</LinearLayout>

</RelativeLayout>

 

Adding An Identifier

The first button in Listing 4.4 includes the following id attribute so that it can be referenced from the code.

android:id=”@+id/cancelButton

 

The plus sign (+) after @ indicates that the identifier (in this case, cancelButton) is being added with this declaration and is not declared in a resource file.

Leave a Reply

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