The Wayback Machine - https://web.archive.org/web/20240815050057/https://www.geeksforgeeks.org/relative-layout-in-android/
Open In App

Relative Layout in Android

Last Updated : 13 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Relative Layout is used to arrange the Child Views in a proper order which means arranging the child objects relative to each other. Generally, if we create an application using a linear layout that consists of 5 buttons. Even if we specify weight and gravity properties they will not be relatively arranged. To arrange them in a proper order we need to use the relative layout. To arrange them we need some advanced properties. Basically, we use layout_width, layout_height, and layout_text properties while creating an application using the linear layout. But we need some more advanced properties which are supported by relative layout.

There are so many properties that are supported by relative layouts. some of the most used properties are listed below

  • layout_alignParentTop
  • layout_alignParentBottom
  • layout_alignParentRight
  • layout_alignParentLeft
  • layout_centerHorozontal
  • layout_centerVertical
  • layout_above
  • layout_below

Step-by-Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java/Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Left Button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Right Button"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom Left Button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"/>
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bottom Right Button"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>
    <Button
        android:id="@+id/button5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Middle Button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java/ MainActivity.kt file and refer to the following code. Below is the code for the MainActivity file. There is nothing to write inside the MainActivity file.

Java
package com.example.example1;

import androidx.appcompat.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);
    }
}
Kotlin
package com.example.android_relative_layout_kotlin

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Output:

Relative_layout_Android

Explanation of the above Program:

  • First, we have written xml open tag which contains xml version and encoding type.
  • In the next line, we have written an open tag for relative layout.
  • Inside the relative layout, we have specified layout width, height, and orientation.
  • Then we have created 5 buttons which are arranged at topRight, topLeft, bottonRight, bottomLeft, bottomRight, and center respectively.
  • Finally, we have closed the relative layout tag.
  • In the java code, there are just import statements.
  • And in the main class, there is an onCreate() method. Which is used to create the activity.
  • Activity is just a user interface.

Click Here to Learn More about Android Development App



Similar Reads

How to Collapse Toolbar Layout in Android?
In this article, we are going to create the CollapsingToolbar app that is fascinating and much useful. CollapsingToolbarLayout gives the facility of adjusting the size of toolbar title text when it is expanded or contracted. A sample GIF is given below to get an idea about how CollapsingToolbarLayout looks like. Step by Step Implementation Step 1:
4 min read
Motion Layout Button in Android Jetpack Compose
Motion Layout is a special version of Constraint layout. With the help of motion layout, we can add animations to the widgets within the layout and change the position of that widget dynamically. In this article, we will take a look at How to implement Motion Layout animation on buttons in Android using Jetpack compose. What we are going to build i
8 min read
How to Create Landscape Layout in Android Studio?
In Android, whenever the user switches to landscape mode an issue is encountered in which some of the widgets become invisible (as you can see in the below image) and so in this scenario, there is a need to design a separate layout for the landscape mode. So in android, every application is designed in almost both the orientations i.e Portrait and
3 min read
How to Use Material Text Input Layout in Android?
Material Design Components (MDC Android) offers designers and developers a way to implement Material Design in their Android application. Developed by a core team of engineers and UX designers at Google, these components enable a reliable development workflow to build beautiful and functional Android applications. If you like the way how the UI ele
3 min read
How to Implement Modal Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Modal Message Layout. A sample image is given below to get an idea
3 min read
How to Implement Image only Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Image only Message Layout. A sample image is given below to get an
3 min read
How to Implement Top banner Message Layout of Firebase In-App Messaging in Android?
Prerequisite: How to Push Notification in Android using Firebase In-App Messaging? We have seen the implementation of Firebase In-App Messaging in Android. In that article, we have implemented the Card Message Layout. In this article, we will take a look at the implementation of the Top banner Message Layout. A sample image is given below to get an
3 min read
What is tools:context in Android Layout Files?
Android Studio provides a much richer variety of functions in the form of various XML attributes in the tools namespace which enables design-time features or compile-time behaviors. When the app is being built, the build tools remove these attributes so that there is no effect on runtime behavior or APK size. tools:context is such an attribute that
2 min read
Flexbox-Layout in Android
Before playing, let's learn about what FlexBox actually is. Hey, do you know the front-end also? If yes, then you should know what actually flexbox is in CSS. To learn more about CSS Flexible Box Layout Module click here. And if not, then don't worry; here is the explanation: Here the word "Flex" stands for Flexible. In this layout, items will "fle
5 min read
Different Ways to Fix "Android Studio Does not Show Layout Preview"
Have you ever faced the problem in the android studio that it is not showing the layout preview while you are constructing a layout for your project? If yes, then this article will help you to solve this problem. Below is the screenshot of the empty layout which is the problem that we have solved: Methods to Solve the Problem There are some methods
3 min read
Implement RadioButton with Custom Layout in Android
RadioButtons allow the user to select one option from a set. You should use RadioButtons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. In this article, we are going to see how we can implement RadioButton with custom layout in android. What we are going to build in this art
3 min read
Tab Layout in Android using Jetpack Compose
Tab Layout is seen used in most applications such as WhatsApp in which users can navigate to multiple screens easily by simply swiping to the left or right. This tab layout provides easy navigation between multiple screens. In this article, we will take a look at How we can implement Tab Layout within our Android application using Jetpack Compose.
7 min read
How to Create Dialog with Custom Layout in Android?
In Android, A dialog is a small window that prompts the user to make a decision, provide some additional information, and inform the user about some particular task. The following are the main purposes or goals of a dialog To warn the user about any activity.To inform the user about any activity.To tell the user whether it is an error or not.To tel
3 min read
How to Center a Button in a Linear Layout in Android?
LinearLayout is a view group that aligns all children vertically or horizontally in a single direction. The "android:orientation" parameter allows you to define the layout direction. A button is a component of the user interface that can be tapped or clicked to carry out an action. Nowadays while developing one of our applications, we wanted to hav
2 min read
How to Make a Layout with Rounded Corners in Android?
In this article, we will learn about How we can Make a Layout With rounded corners with the help of Drawables. We can create and use any type of shape for different UI Components because of the Flexibility of the Android. Various attractive designs for creating user interfaces can be created using a layout with rounded corners like: For login Desig
3 min read
Android Layout Editor - Code, Split And Design
Android Layout Editor is the workplace in Android Studio, in which we do the design part of an android app by just dragging UI elements into the editor instead of writing XML code for it. Android Studio Layout Editor enables us to build layouts by dragging components onto the screen or editing an XML layout file. Android Studio Layout Editor, does
2 min read
Android- Hide or Show Specific Layout & Views Efficiently
In Android app development, managing the visibility of various layout and view components is a common task. To make buttons, text, images, and other elements appear/disappear based on user input, in some long process tasks or show different page layouts depending on the app's state, developers often have to write repetitive code to handle these sce
4 min read
Absolute Layout in Android with Example
An Absolute Layout allows you to specify the exact location i.e. X and Y coordinates of its children with respect to the origin at the top left corner of the layout. The absolute layout is less flexible and harder to maintain for varying sizes of screens that's why it is not recommended. Although Absolute Layout is deprecated now. Some of the impor
2 min read
Android Motion Layout in Kotlin
MotionLayout is a special version of ConstraintLayout. It is a layout that helps to manage motion and widget animations in the app. Since it is a special version of ConstraintLayout, it is made as a sub-class of ConstraintLayout. It provides touch control motion to the app by describing how to transition between different layouts. In a nutshell, it
5 min read
Android Manifest File in Android
Every project in Android includes a Manifest XML file, which is AndroidManifest.xml, located in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements. This file includes nodes for each of the Activities, Se
5 min read
Android | Android Application File Structure
It is very important to know about the basics of Android Studio's file structure. In this article, some important files/folders, and their significance is explained for the easy understanding of the Android studio work environment. In the below image, several important files are marked: All of the files marked in the above image are explained below
3 min read
What's Android Interface Definition Language (AIDL) in Android?
The Android Interface Definition Language (AIDL) is analogous to other IDLs you would possibly have worked with. It allows you to define the programming interface that both the client and repair agree upon so as to speak with one another using interprocess communication (IPC). Traditionally the Android way a process cannot access the memory of some
7 min read
Android | Running your first Android app
After successfully Setting up an Android project, all of the default files are created with default code in them. Let us look at this default code and files and try to run the default app created. The panel on the left side of the android studio window has all the files that the app includes. Under the java folder, observe the first folder containi
3 min read
How to Clone Android Project from GitHub in Android Studio?
Android Studio provides a platform where one can build apps for Android phones, tablets, Android Wear, Android TV, and Android Auto. Android Studio is the official IDE for Android application development, based on IntelliJ IDEA. One can develop Android Applications using Kotlin or Java as the Backend Language, and it provides XML for developing Fro
3 min read
How to Add OpenCV library into Android Application using Android Studio?
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in today’s systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. When it integrated with various libraries,
3 min read
8 Best Android Libraries That Every Android Developer Should Know
Android is an operating system which is built basically for Mobile phones. It is used for touchscreen mobile devices like smartphones and tablets. But nowadays these are utilized in Android Auto cars, TV, watches, camera, etc. Android has been one of the best-selling OS for smartphones. Android OS was developed by Android Inc. which Google bought i
5 min read
How to Add Audio Files to Android App in Android Studio?
The audio file format is a file format for saving digital audio data on a computer system and all are aware of audio files. So in this article, we are going to discuss how can we add audio files to the android app. There are three major groups of audio file formats: Format Name Description Examples Uncompressed audio formatsUncompressed audio forma
3 min read
Different Ways to Change Android SDK Path in Android Studio
Android SDK is one of the most useful components which is required to develop Android Applications. Android SDK is also referred to as the Android Software Development Kit which provides so many features which are required in Android which are given below: A sample source code.An Emulator.Debugger.Required set of libraries.Required APIs for Android
5 min read
How to Capture a Screenshot and Screen Recording of an Android Device Using Android Studio?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrains’ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. Whenever we are developing an app and in that device, you don't know how to capture screenshot
2 min read
Different Ways to Fix “Select Android SDK” Error in Android Studio
Android SDK is one of the most useful components which is required to develop Android Applications. Android SDK is also referred to as the Android Software Development Kit which provides so many features which are required in Android which are given below: A sample source code.An Emulator.Debugger.Required set of libraries.Required APIs for Android
5 min read
three90RightbarBannerImg