Table of Contents
Introduction
The RelativeLayout allows you to position the child views in relation to either each other or to their parent. If you have placed a EditView, TextView, ImageView, etc at some place in your activity or layout, then in the RelativeLayout, you can arrange the elements (Button, ToggleButton, ImageButton, etc) to the left, right, or below the EditView, TextView, ImageView, etc.
Create activity_relative_layout file in XML
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/content_main” // only apply in navigation menu application
android:layout_width=”match_parent” // cover full screen
android:layout_height=”match_parent” // cover full screen
android:paddingBottom=”16dp” // It is used to give space from bottom screen
android:paddingLeft=”16dp” // It is used to give space from left screen
android:paddingRight=”16dp” // It is used to give space from right screen
android:paddingTop=”16dp”> // It is used to give space from top screen
<TextView
android:id=”@+id/textView” // Every element has a ID in android studio.
android:layout_width=”wrap_content” // cover the width of the text view, not full screen
android:layout_height=”wrap_content” // cover the height of the text view, not full screen
android:textSize=”20sp”
android:text=”This is the Text View”
android:layout_marginTop=”20dp”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true” /> // make a text view in center of a screen
<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Button One”
android:layout_below=”@+id/textView”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”40dp” />
<Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Button Two”
android:layout_alignParentStart=”true”
android:layout_alignParentLeft=”true”
android:layout_centerInParent=”true”
android:layout_marginTop=”280dp” />
<Button
android:id=”@+id/button3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Button Three”
android:layout_centerVertical=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentEnd=”true”
android:layout_marginTop=”280dp” />
<SeekBar
android:id=”@+id/seekBar”
android:layout_marginBottom=”50dp”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true” />
</RelativeLayout>
Create relativeLayout File in Java
package com.eiheducation.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class relativeLayout extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative_layout);
}
}
Android Manifest file
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>
<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.relativeLayout”></activity>
</application>
</manifest>
Output
