Skip to content
Related Articles

Related Articles

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

XML



filter_none

edit
close

play_arrow

link
brightness_4
code

<AbsoluteLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<!--add child views-->
</AbsoluteLayout>

chevron_right


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

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">
  
    <!--Setting up TextViews-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="100px"
        android:layout_y="300px" />
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="120px"
        android:layout_y="350px" />
  
</AbsoluteLayout>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Step 3: Working with the MainActivity.java file

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

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
  
public class MainActivity extends AppCompatActivity {
  
    TextView heading, subHeading;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Referencing the TextViews
        heading = (TextView) findViewById(R.id.heading);
        subHeading = (TextView) findViewById(R.id.subHeading);
          
        // Setting text dynamically
        heading.setText("Computer Science Portal");
        subHeading.setText("GeeksForGeeks");
    }
}

chevron_right


Output: Run On Emulator

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

Absolute Layout in Android

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :