Android Absolute Layout Example

Android AbsoluteLayout is used to layout ui components with absolute x, y axis coordinate value. 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 specific location. This example will show you how to use it.

1. Android Absolute Layout Example.

There are two textview, two edittext and two button widget in the example. All the ui components is placed in AbsoluteLayout. So each ui components location is specified by it’s layout_x and layout_y attribute value.

When click the save button, it will popup a toast message at screen bottom. When click the cancel button, the input text box content will be reset.

Below is the example files list panel.

absolute layout source files list

2. Main Activity Java File.

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("");
            }
        });

    }
}

2. Main Activity Layout Xml File.

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>
(Visited 94 times, 1 visits today)
READ :   Android LinearLayout Example

Leave a Reply

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.