Attributes in Absolute Layout
The following are the most significant AbsoluteLayout attributes:
- android:id: This is the unique identifier(specific ID) for the layout.
- android:layout_x: The view's x-coordinate is specified here.
- 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!