Skip to content
Related Articles

Related Articles

Android RelativeLayout in Kotlin
  • Last Updated : 20 Jul, 2020

Android RelativeLayout is a ViewGroup subclass, used to specify the position of child View elements relative to each other like (A to the right of B) or relative to the parent (fix to the top of the parent).

Instead of using LinearLayout, we have to use RelativeLayout to design the user interface and keep our hierarchy flat because it improves the performance of the application.

Important Attributes for positioning views in the RelativeLayout

As we know, we need to define the position of child views or ViewGroups relative to each other element or relative to the parent. By default position is top-left, if someone forgets to specify the position of child views.

XML attributesDescription
layout_alignParentLeftIt is set “true” to match the left edge of view to the left edge of parent.
layout_alignParentRightIt is set “true” to match the right edge of view to the right edge of parent.
layout_alignParentTopIt is set “true” to match the top edge of view to the top edge of parent.
layout_alignParentBottomIt is set “true” to match the bottom edge of view to the bottom edge of parent.
layout_alignLeftIt accepts another sibling view id and align the view to the left of the specified view id
layout_alignRightIt accepts another sibling view id and align the view to the right of the specified view id.
layout_alignStartIt accepts another sibling view id and align the view to start of the specified view id.
layout_alignEndIt accepts another sibling view id and align the view to end of specified view id.
layout_centerInParentWhen it is set “true”, the view will be aligned to the center of parent.
layout_centerHorizontalWhen it is set “true”, the view will be horizontally centre aligned within its parent.
layout_centerVerticalWhen it is set “true”, the view will be vertically centre aligned within its parent.
layout_toLeftOfIt accepts another sibling view id and places the view left of the specified view id.
layout_toRightOfIt accepts another sibling view id and places the view right of the specified view id.
layout_toStartOfIt accepts another sibling view id and places the view to start of the specified view id.
layout_toEndOfIt accepts another sibling view id and places the view to end of the specified view id.
layout_aboveIt accepts another sibling view id and places the view above the specified view id.
layout_belowIt accepts another sibling view id and places the view below the specified view id.

How to declare RelativeLayout in XML file?

First of all, we should declare the RelativeLayout in layout file using the below code.




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">
  
    // Add other view or ViewGroup here
</RelativeLayout>

RelativeLayout in activity_main.xml file

Following is the code for RelativeLayout in xml file.






<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">
     
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="First name:"
        android:layout_marginTop="20dp"
        android:textSize="20dp"/>
  
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textView1"
        android:layout_marginTop="8dp"/>
      
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="10dp"
        android:text="Last name:"
        android:textSize="20dp"/>
  
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@id/textView2"
        android:layout_marginTop="45dp"/>
  
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/textView2"
        android:layout_marginTop="20dp"
        android:text="Submit" />
      
</RelativeLayout>

MainActivity.kt file

When we have created layout, we need to load the XML layout resource from our activity onCreate() callback method and access the UI element form the XML using findViewById.




package com.geeksforgeeks.myfirstKotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // below access the UI elements
  
  
    }
}

RelativeLayout Output:

We can run the application using the Android Virtual Device(AVD) to get the output of the above code.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :