Android RelativeLayout in Kotlin - GeeksforGeeks

16 Mar, 2022
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.
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.
First of all, we should declare the RelativeLayout in layout file using the below code.
XML
<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>
Following is the code for RelativeLayout in xml file.
XML
<?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>
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.
Kotlin
package com.geeksforgeeks.myfirstKotlinapp  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundle    class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // below access the UI elements        }}
We can run the application using the Android Virtual Device(AVD) to get the output of the above code.
Akanksha_Rai
ayushpandey3july
Kotlin Android
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Retrofit with Kotlin Coroutine in Android
Android Menus
Kotlin Android Tutorial
MVP (Model View Presenter) Architecture Pattern in Android with Example
Kotlin Setters and Getters
MVC (Model View Controller) Architecture Pattern in Android with Example
Kotlin Coroutines on Android