Skip to content
Related Articles

Related Articles

Improve Article

Absolute Layout in Android with Example

  • Last Updated : 02 Dec, 2020

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 is in density-pixel or pixel)
  3. android:layout_y: It specifies Y-Coordinate of the Views (Possible values of this is in dp or px)

The Syntax for Absolute Layout


<!--add child views-->


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_mian.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Setting up TextViews-->
        android:layout_y="300px" />
        android:layout_y="350px" />

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.  


    <color name="colorPrimary">#0F9D58</color
    <color name="colorPrimaryDark">#16E37F</color
    <color name="colorAccent">#03DAC5</color

Step 3: Working with the file

In this step, we will initialize the TextViews in our file.


import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    TextView heading, subHeading;
    protected void onCreate(Bundle savedInstanceState) {
        // Referencing the TextViews
        heading = (TextView) findViewById(;
        subHeading = (TextView) findViewById(;
        // Setting text dynamically
        heading.setText("Computer Science Portal");

Output: Run On Emulator

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

Absolute Layout in Android

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :