Relative Layout in Android with Example

Android | Relative Layout: In this tutorial, we will learn about Relative Layout with the help of an example, and its uses by working with two edit texts respectively. By Manu Jemini Last updated : June 08, 2023

What is Relative Layout in Android?

Relative Layout is one of the most common layouts in Android layouts. In this layout, the different entities are placed in comparison with other entities. For example, if you placed a button somewhere in the layout, it will be placed in accordance with its distance from left, right or top entities.

How to Implement Relative Layout in Android?

This is very simple to implement because you don't have set margins and orientations. You just have to place wherever you want it to be.

The Major drawback it faces is that whenever your layout got complex or it comprises of so many entities, then if you add or move a particular piece, the whole layout could get affected. Because, entities are placed in a manner which says, like 100dp left from that particular entity, so when that entity moved, this entity also has to move to maintain 100dp distance to it.

There are few situations when using relative layout is beneficial as compared to other layouts. One of the most common is log-in and log-out layouts. It will be very quick to place two EditText and a Button anywhere you desire to place.

Android Relative Layout Example

Below code is an example of using it, to get started from nothing, make an android project. An activity will automatically be created for you; make use of that and alongside with it a layout file.

Java file

package numeric.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
  
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="numeric.test.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/_email"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="83dp"
        android:hint="Enter Email" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/_password"
        android:hint="Enter Password"
        android:layout_below="@+id/_email"
        android:layout_alignStart="@+id/_email"
        android:layout_marginTop="39dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/_login_btn"
        android:layout_below="@+id/_password"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="59dp" />

</RelativeLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="numeric.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

Relative layout


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.