Android Absolute Layout Example

Android AbsoluteLayout is used to layout UI components with absolute x, y-axis coordinate values. For example, if you put a button widget in AbsoluteLayout, you can set the button’s layout_x, and layout_y property value to place the button in a specific location. This example will show you how to use it.

1. Android Absolute Layout Example.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/2Fax3HREmGw

  1. There are two TextView, two EditText, and two Button widgets in the example.
  2. All the UI components are placed in AbsoluteLayout.
  3. So each UI component’s location is specified by it’s layout_x and layout_y attribute value.
  4. When clicking the SAVE button, it will pop up a toast message at the screen bottom.
  5. When clicking the CANCEL button, the input text box content will be reset.
  6. Below is the example files list.
    D:\WORK\DEV2QA.COM-EXAMPLE-CODE\ANDROIDEXAMPLEPROJECT\ABSOLUTELAYOUT
    │
    ├─app
    │  │  .gitignore
    │  │  build.gradle
    │  │  proguard-rules.pro
    │  │
    │  └─src
    │      │
    │      ├─main
    │      │  │  AndroidManifest.xml
    │      │  │
    │      │  ├─java
    │      │  │  └─com
    │      │  │      └─dev2qa
    │      │  │          └─absolutelayout
    │      │  │                  MainActivity.java
    │      │  │
    │      │  └─res
    │      │      │
    │      │      ├─layout
    │      │      │      activity_main.xml
    │      │      │

2. Main Activity Java File.

  1. MainActivity.java
    package com.dev2qa.absolutelayout;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            setTitle("dev2qa.com - Android Absolute Layout Example.");
    
            final EditText userNameEditText = (EditText)findViewById(R.id.userName);
    
            final EditText passwordEditText = (EditText)findViewById(R.id.password);
    
            Button saveButton = (Button)findViewById(R.id.saveButton);
            saveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String userName = userNameEditText.getText().toString();
                    String password = passwordEditText.getText().toString();
    
                    StringBuffer stringBuffer = new StringBuffer();
    
                    stringBuffer.append("User Name : ");
                    stringBuffer.append(userName);
                    stringBuffer.append(", Password : ");
                    stringBuffer.append(password);
    
                    Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_LONG).show();
                }
            });
    
            Button cancelButton = (Button)findViewById(R.id.cancelButton);
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    userNameEditText.setText("");
                    passwordEditText.setText("");
                }
            });
    
        }
    }

3. Main Activity Layout XML File.

  1. app / res / layout / activity_main.xml
    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/saveButton"
            android:layout_x="100dp"
            android:layout_y="150dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save" />
    
        <Button
            android:id="@+id/cancelButton"
            android:layout_x="200dp"
            android:layout_y="150dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cancel" />
    
        <EditText
            android:id="@+id/userName"
            android:layout_x="100dp"
            android:layout_y="60dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Input User Name" />
    
        <EditText
            android:id="@+id/password"
            android:layout_x="100dp"
            android:layout_y="100dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Input Password"
            android:ems="10"
            android:inputType="textPassword" />
    
        <TextView
            android:layout_x="20dp"
            android:layout_y="80dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="User Name : " />
    
        <TextView
            android:layout_x="20dp"
            android:layout_y="120dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Password" />
    </AbsoluteLayout>

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.