Android RelativeLayout Example

Android RelativeLayout Example

Android RelativeLayout is one of the most used design after LinearLayout. It allows its child views to put relative to the other person or in accordance with the box or another pot. For instance, if we must build a complicated structure as shown in the image below, this is achieved  easily by using RelativeLayout.

android relativelayout example

Placement Views in Android RelativeLayout

As reviewed above, the RelativeLayout childs need to designate their position in accordance with the parent or guardian view or another siblings. If indeed they don’t identify their position explicitly, by default they sit to the top-left place of parent design.

Android RelativeLayout Properties

A number of the many structure properties open to views in a RelativeLayout include:

android:layout_alignParentTop
If “true”, makes the very best edge of the view match the most notable advantage of the parent.
android:layout_below
Positions the very best edge of the view below the view given with a source ID.
android:design_toRightOf
Positions the still left edge of the view to the right of the view given with a reference ID.
android:layout_centerVertical
If “true”, centers this child vertically within its parent.

Android RelativeLayout Examples

Why don’t we create a good example of RelativeLayout as shown in the screenshot below. With regard to simplicity, we’ve made the example relatively very easy, with the addition of some random Control keys to show different Android RelativeLayout qualities.

android relativelayout example


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


Below Is the Source code of above example.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Button5" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button2"
        android:layout_centerHorizontal="true"
        android:text="Button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button4"
        android:layout_centerHorizontal="true"
        android:text="Button6" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_toEndOf="@+id/button"
        android:layout_toRightOf="@+id/button"
        android:text="Button7" />

</RelativeLayout>

 

See Also:

Android RecyclerView

Android CardView

Android ListView


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed