Android Codingprogrammingtechnology

Android Coding: Relative Layout

A Relative Layout is a view group that displays child views in relative positions. The position of each view can be specified as relative-to-sibling elements (such as to the left-of or below another view). In contrast, they can be seen in positions relative to the parent Relative Layout area (such as aligned to the bottom, left, or center).

relative layout

This view group is a very powerful utility for designing a user interface because it can eliminate nested view groups. It also keeps layouts hierarchies flat, which improves performance. Rather than using several nested Linear Layout groups, one may be able to replace them with a single Relative Layout.

Positioning Views

Relative Layout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by the right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout.  Hence, you must define the position of each view using the various layout properties.

Some of many layout properties available to views in a Relative Layout include:

android:layout_alignParentTop

If “true”, makes the top edge of this view match the top edge of the parent.

android:layout_centerVertical

If “true”, centers this child vertically within its parent.

android:layout_below

Positions the top edge of this view below the view specified with a resource ID.

android:layout_toRightOf

Positions the left edge of this view to the right of the view specified with a resource ID.

These are just a few examples. All layout attributes are documented at RelativeLayout.LayoutParams.

The value for each layout property is either a Boolean to enable a layout position relative to the parent Relative Layout or an ID that references another view in the layout against which the view should be positioned.

In your XML layout, you can declare dependencies against other views in the layout in any order. For example, you can declare that “view1” positions below “view2” even if “view2” is the last view component or layout position declared in the hierarchy.

Many consider Relative Layout a very flexible layout, with great layout properties and position in android for custom layout design. This is because in android, it gives the flexibility to position components/views based on the relative or sibling component’s position. It is the most used layout after the Linear Layout in Android.

 

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

Leave a reply

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

Next Article:

0 %