Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Absolute Layout?
2.1.
Need of Absolute Layout:
2.2.
Advantages of Absolute Layout:
2.3.
Disadvantages of Absolute Layout:
3.
Attributes in Absolute Layout
4.
Example Application 
5.
Frequently Asked Questions
5.1.
1. Why is the absolute layout no longer recommended?
5.2.
2. Is there any better alternate for Absolute layout?
5.3.
3. What do you mean by containers and layouts in Android?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Absolute Layout in Android with Example

Author Akshit Pant
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The absolute layout is a layout that allows you to specify the exact placement of its children(widgets) with x/y coordinates. Absolute layouts are less flexible and more challenging to manage than layouts that do not use absolute placement.

Before jumping directly on how Android Studio does this, first knowing the basic knowledge of Kotlin and how to use it is strongly recommended.

So, let us start reading about how Absolute layout works in Android.

What is Absolute Layout?

We can specify the exact place with an Absolute Layout. i.e., the X and Y coordinates of its offspring in relation to the layout's origin in the top left corner.

Need of Absolute Layout:

The motive of using the Absolute layout was to inform the widget about the relative positioning on the screen with other widgets.

Advantages of Absolute Layout:

The only advantage of using an Absolute layout is that it gives us x/y coordinates to place our desired widgets in our layout in a relative fashion with each other.

Disadvantages of Absolute Layout:

Absolute layouts are more difficult to maintain for various mobile screen sizes than other layout kinds because we specify the precise placement of a child view/widget. The use of Absolute layout is deprecated(not recommended), Since the positioning is based on x/y coordinates, which isn't very useful in the world of different screen sizes and aspect ratios.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Attributes in Absolute Layout

The following are the most significant AbsoluteLayout attributes:

  1. android:id: This is the unique identifier(specific ID) for the layout.
  2. android:layout_x: The view's x-coordinate is specified here.
  3. android:layout_y: The view's y-coordinate is specified here.

Note: The x and y coordinates are taken with reference from the origin(top-left of the screen).

Example Application 

Now let us see How we use Absolute layout in our project with a simple example given below in which we will be placing different widgets(TextView, Button, ImageViews) in absolute relation to each other(giving widgets their x/y coordinates); for this, open up the Android Studio, start an empty activity, give a name to your app(Kotlin Absolute layout CN in this case), and let Android do the Gradle syncing.

MainActivity.kt

package com.codingninjas.kotlinabsolutelayoutcn

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)
    }
}

Let the main activity be in the usual form; we can create and set text and image type dynamically in the main activity file, but for this time, let us do the work from the XML file only.

Now make the activity_main.xml file as per the need of your desired layout.

activity_main.xml:
 

<?xml version="1.0" encoding="utf-8"?>
<!-- using absolute lay(not recommended)-->
<AbsoluteLayout
    android:id="@+id/myAbsoluteLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/heading"
        android:text="@string/this_is_user_profile"
        android:gravity="center"
        android:textSize="25sp"
        android:layout_x="0dp"
        android:layout_y="0dp" />

    <ImageView
        android:id="@+id/zoomIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="26dp"
        android:layout_y="259dp"
        app:srcCompat="@android:drawable/btn_plus" />

    <ImageView
        android:id="@+id/zoomOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="299dp"
        android:layout_y="266dp"
        app:srcCompat="@android:drawable/btn_minus" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_x="305dp"
        android:layout_y="628dp"
        app:srcCompat="@android:drawable/ic_menu_send" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="16dp"
        android:layout_y="666dp"
        android:padding="5dp"
        android:text="@string/save_to_favourite" />

    <ImageView
        android:id="@+id/celebImage"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_x="98dp"
        android:layout_y="263dp"
        app:srcCompat="@android:drawable/ic_lock_idle_low_battery" />

</AbsoluteLayout>

 

Output:

 

 

Here, we have created four image views, a text view, and a button inside our Absolute layout and have given them x/y coordinates. Use this exact code in your own compiler and see the difference in layout view in your own device so that you can conclude yourself how Absolute layout changes from device to device(size and aspect ratio) and why it’s deprecated.

Frequently Asked Questions

1. Why is the absolute layout no longer recommended?

Ans:  Absolute layout is deprecated since It is incompatible with various screen resolutions(different screen sizes).

2. Is there any better alternate for Absolute layout?

Ans: Yes, We can use a Relative layout instead of an Absolute layout. As the name implies, views are arranged in relation to one another in a Relative layout.

3. What do you mean by containers and layouts in Android?

Ans: Containers are used to group objects and widgets together. If you have a lot of widgets, you'll need a container with a root element to put them all in. On the other hand, Android comes with a set of view classes that act as view containers, and Layouts is the name for these container classes.

Conclusion 

In this article, we learned about Absolute layout and its implementation. We also infer from this article why it’s deprecated and what’s a better alternative.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope this article has helped you enhance your knowledge of the Android UI Absolute layout. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.
Happy Coding!

Previous article
MVVM (Model View ViewModel) Architecture Pattern in Android
Next article
Difference between MVC, MVP, and MVVM Architecture in Android
Live masterclass