
An Absolute Layout lets you specify exact locations (x/y coordinates) of its children. Absolute layouts are less flexible and harder to maintain than other types of layouts without absolute positioning.
Note: Now this became deprecation so we are not using now.
AbsoluteLayout Attributes
Following are the important attributes specific to AbsoluteLayout –
android:id This is the ID which uniquely identifies the layout.
android:layout_x This specifies the x-coordinate of the view.
android:layout_y This specifies the y-coordinate of the view.
MainActivity.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.example.demo; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } } |
Following will be the content of res/layout/activity_main.xml file −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="OK" android:layout_x="50px" android:layout_y="361px" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="Cancel" android:layout_x="225px" android:layout_y="361px" /> </AbsoluteLayout> |
Following will be the content of res/values/strings.xml to define two new constants −
1 2 3 4 5 |
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">demo</string> <string name="action_settings">Settings</string> </resources> |
Let’s try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android Studio, open one of your project’s activity files and click Run icon from the toolbar. Android Studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −