Absolute Layout in Android

Absolute Layouts are used to create custom layouts in Android. You can use x and y coordinates in this layout to describe the precise location of its children. This article will teach you everything you need to know about Android’s Absolute Layout.

Layouts

An Android layout is a class that handles arranging the way its children appear on the screen. Anything that is a View (or inherits from View) can be a child of a layout. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts. You could also create your own custom layout by making a class that inherits from ViewGroup.

If you want to enrich your career and become a professional in Android, then visit Mindmajix - a global online training platform: "Android Training" .This course will help you to achieve excellence in this domain.

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:

  • Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.
  • Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

The standard Layouts are:

  1. AbsoluteLayout
  2. FrameLayout
  3. LinearLayout
  4. RelativeLayout
  5. TableLayout

Absolute Layout:-

Absolute Layout is based on the simple idea of placing each control at an absolute position. You specify the exact x and y coordinates on the screen for each control. This is not recommended for most UI development (in fact Absolute Layout is currently deprecated) since absolutely positioning every element on the screen makes an inflexible UI, that is much more difficult to maintain. Consider what happens if a control needs to be added to the UI. You would have to change the position of every single element that is shifted by the new control.

Checkout Android Interview Questions

The absolute layout is less flexible and harder to maintain than a linear layout, relative layout, table layout, etc. To specify views inside absolute layout, you have to use android:layout_x for x-coordinate and android:layout_y for y-coordinate.

Here is a sample Layout XML using AbsoluteLayout.

MindMajix Youtube Channel

Absolute Layout

<Button
android:id="@+id/backbutton"
android:text="Back"
android:layout_x="10px"
android:layout_y="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_x="10px"
android:layout_y="110px"
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /><br><br>
<EditText
android:layout_x="150px"
android:layout_y="100px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_x="10px"
android:layout_y="160px"
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /><br><br>
<EditText
android:layout_x="150px"
android:layout_y="150px"
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /><br><br>
</AbsoluteLayout>

Note how each element has android:layout_x and android:layout_y specified. Android defines the top left of the screen as (0,0) so the layout_x value will move the control to the right, and the layout_y value will move the control down. Here is a screenshot of the layout produced by this XML.

 

Demo

Explore Android Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!
Course Schedule
NameDates
Android TrainingOct 12 to Oct 27View Details
Android TrainingOct 15 to Oct 30View Details
Android TrainingOct 19 to Nov 03View Details
Android TrainingOct 22 to Nov 06View Details
Last updated: 03 Apr 2023
About Author

Ravindra Savaram is a Technical Lead at Mindmajix.com. His passion lies in writing articles on the most popular IT platforms including Machine learning, DevOps, Data Science, Artificial Intelligence, RPA, Deep Learning, and so on. You can stay up to date on all these technologies by following him on LinkedIn and Twitter.

read less