Absolute Layout With Example In Android Studio

In Android, an Absolute Layout is a layout used to design the custom layouts. In this layout you can specify the exact location of its children by using x and y coordinates.

Absolute Layout in Android Studio
Important Note 1: Absolute layout are harder to maintain for different mobile screen sizes than other types of layouts because we set the exact location of a child view or called component. The positioning is based on x(top) and y(left) coordinates and that positioning is not as useful in world of various screen resolutions(sizes) and aspect ratios.

Important Note 2: Absolute layout is depreciated in Android because of the same reason as discussed in above note.


Android Absolute Layout Syntax Code:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- add child view’s here -->

</AbsoluteLayout>

Attributes of Absolute Layout:

1.id: In android id is a attribute used to uniquely identify a Absolute Layout.
Below is id attribute’s example code with explanation included.

<AbsoluteLayout
android:id="@+id/absoluteLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

2.layout_x: In Absolute Layout layout_x attribute is used to specify the x- coordinate of the view(TextView or any other view). The possible value for this is in dp or px.

layout_x example in Absolue Layout

Below is the layout_x attribute’s example code with explanation included in which we set the 200px value for the x-coordinate.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:textSize="25sp"
android:layout_x="200px"/> <!--x-coordinates of a text view in AbsoluteLayout-->

3.layout_y: In AbsoluteLayout layout_y attribute is used to specify the y- coordinate of the view(TextView or any other view). The possible value for this is in dp or px.

layout_y example in Absolute Layout

Below is the layout_y attribute’s example code with explanation included in which we set the 200px value for the y- coordinate.

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AbhiAndroid"
android:textSize="25sp"
android:layout_y="200px"/><!--y-coordinate of a text view in Android Studio-->

Example of Absolute Layout in Android:

Below is the example code of Absolute Layout in which we design a login screen with two field user name and password and one button for login. We set the all views using x and y coordinates of the screen and set the values in px(pixels). Below is the final output and code:

Download Code
Take Less Than A Minute To Download

Enter Your Email Address And Get Free Download Of Absolute Layout Code in Android Studio

Free Instant Download
You will be redirected to dropbox where you can download code

Absolute Layout Example
Step 1: Create a new project and name it AbsoluteLayoutExample.

Select File -> New -> New Project. Fill the forms and click “Finish” button.

Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add following code. Here we are designing a login form inside Absolute Layout.

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_x="110px"
        android:layout_y="110px"
        android:text="User Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_x="250px"
        android:layout_y="80px"
        android:width="100px"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_x="110px"
        android:layout_y="200px"
        android:text="Password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:layout_x="250px"
        android:layout_y="150px"
        android:width="100px"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Log In"
        android:layout_x="300px"
        android:layout_y="300px"/>
</AbsoluteLayout>

Step 3: Now Open java -> package -> MainActivity.java and paste the below code.

package example.abhiandriod.absolytelayoutexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Step 4: Now Open Manifests and click on AndroidManifest.xml and paste the below code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.abhiandriod.absolytelayoutexample" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5: Lastly Open res -> values ->strings.xml and paste the below code.

<resources>
    <string name="app_name">AbsolyteLayoutExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
</resources>

Output:

Now run the App in Emulator or AVD. You will see the login form designed in Absoulte Layout having fix x and y coordinate.

Login Form Using Absolute Layout Example
Important Note: Try opening the same layout in different size Emulator and you will see it doesn’t fit in different size device. That’s why Android has depreciated the use of Absolute Layout.

  • Is this article helps you understand?
  • YES   NO

Leave a Reply

Your email address will not be published. Required fields are marked *



Continue Reading Below Tutorial:

Subscribe and get access to the latest android tutorials, freebies, and lots more!

Let’s Connect

Abhishek Saini Hi, I'm Abhishek, founder and owner of this site. I believe in team work and there is a experienced developer team working on AbhiAndroid with a mission to simplify learning of Android App Development.
CONNECT WITH ME:
FACEBOOK | LINKEDIN | INSTAGRAM
Android Developer Money eBook
One More Step...

Enter your email and I'll send you a FREE copy of Android Dev Dollaring eBook:
Send Me This eBook >>
We hate SPAM and promise to keep your email address safe.
Take Less Than A Minute To Download

Enter Your Email Address And Get Free Download Of Absolute Layout Code in Android Studio

Free Instant Download
You will be redirected to dropbox where you can download code

Download Your Free Copy

Download this free eBook and Learn:
1. Different approach developer use to earn through App business
2. Learn how Android Developer can generate passive income
3. Job is not the only option and you can choose freelancing
Send Me This eBook >>
One More Step...

Enter your email and I'll send you a FREE copy of Android Dev Dollaring eBook:
Send Me This eBook >>
We hate SPAM and promise to keep your email address safe.