Absolute Layout in Android with Example
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 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
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.
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.
Step 3: Working with the MainActivity.java file
In this step, we will initialize the TextViews in our MainActivity.java file.
Output: Run On Emulator
You will see that TextViews are having fixed X and Y Coordinates.
Please Login to comment...