Absolute Layout in Android with Example - GeeksforGeeks

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.
Some of the important Absolute Layout attributes are the following:
android:id: It uniquely specifies the absolute layoutandroid:layout_x: It specifies X-Coordinate of the Views (Possible values of this is in density-pixel or pixel)android:layout_y: It specifies Y-Coordinate of the Views (Possible values of this is in dp or px)
android:id: It uniquely specifies the absolute layout
android:layout_x: It specifies X-Coordinate of the Views (Possible values of this is in density-pixel or pixel)
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
<AbsoluteLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"><!--add child views--></AbsoluteLayout>
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 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
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    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>
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
<resources>     <color name="colorPrimary">#0F9D58</color>     <color name="colorPrimaryDark">#16E37F</color>     <color name="colorAccent">#03DAC5</color> </resources> 
Step 3: Working with the MainActivity.java file
In this step, we will initialize the TextViews in our MainActivity.java file.
Java
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");    }}
You will see that TextViews are having fixed X and Y Coordinates.
android
Picked
Technical Scripter 2020
Android
Java
Technical Scripter
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
How to Post Data to API using Retrofit in Android?
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Stream In Java
Object Oriented Programming (OOPs) Concept in Java