The Wayback Machine - https://web.archive.org/web/20250126182625/https://www.geeksforgeeks.org/absolute-layout-in-android-with-example/
Open In App

Absolute Layout in Android with Example

Last Updated : 19 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Absolute Layout in Android

Some of the important Absolute Layout attributes are the following:

  1. android:id: It uniquely specifies the absolute layout
  2. android:layout_x: It specifies X-Coordinate of the Views (Possible values of this are in density-pixel or pixel)
  3. android:layout_y: It specifies Y-Coordinate of the Views (Possible values of this are in dp or px)

The Syntax for Absolute Layout

XML
package com.example.demo;

import android.os.Bundle;
import android.app.Activity;

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

Example

In this example, we are going to create a basic application with Absolute Layout that is having two TextView. Note that we are going to implement this project using the Java language.

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 as the programming language.

Step 2: Create the layout file

For this go to app > res > layout > activity_main.xml file and change the Constraint Layout to Absolute Layout and add TextViews. Below is the code snippet for the activity_main.xml file.

XML
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   
   <Button
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:text="OK"
      android:layout_x="50px"
      android:layout_y="361px" />
   <Button
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:text="Cancel"
      android:layout_x="225px"
      android:layout_y="361px" />

</AbsoluteLayout>


Before moving further let’s add some color attributes in order to enhance the app bar. Go to app > res > values > colors.xml and add the following color attributes.  

XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">demo</string>
   <string name="action_settings">Settings</string>
</resources>

Output: Run On Emulator

You will see that TextViews are having fixed X and Y Coordinates.

absolute1




Next Article

Similar Reads

three90RightbarBannerImg