Added in API level 1

RelativeLayout


open class RelativeLayout : ViewGroup
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.RelativeLayout

A Layout where the positions of the children can be described in relation to each other or to the parent.

Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

Note: In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead.

This behavior has been preserved for apps that set android:targetSdkVersion="17" or older in their manifest's uses-sdk tag for compatibility. Apps targeting SDK version 18 or newer will receive the correct behavior.

See the Relative Layout guide.

Also see RelativeLayout.LayoutParams for layout attributes

Summary

Nested classes
open

Specifies how a view is positioned within a RelativeLayout.

XML attributes
android:gravity Specifies how an object should position its content, on both the X and Y axes, within its own bounds.
android:ignoreGravity Indicates what view should not be affected by gravity.
Inherited XML attributes
Constants
static Int

Rule that aligns a child's bottom edge with another child's top edge.

static Int

Rule that aligns a child's baseline with another child's baseline.

static Int

Rule that aligns a child's bottom edge with another child's bottom edge.

static Int

Rule that aligns a child's end edge with another child's end edge.

static Int

Rule that aligns a child's left edge with another child's left edge.

static Int

Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge.

static Int

Rule that aligns the child's end edge with its RelativeLayout parent's end edge.

static Int

Rule that aligns the child's left edge with its RelativeLayout parent's left edge.

static Int

Rule that aligns the child's right edge with its RelativeLayout parent's right edge.

static Int

Rule that aligns the child's start edge with its RelativeLayout parent's start edge.

static Int

Rule that aligns the child's top edge with its RelativeLayout parent's top edge.

static Int

Rule that aligns a child's right edge with another child's right edge.

static Int

Rule that aligns a child's start edge with another child's start edge.

static Int

Rule that aligns a child's top edge with another child's top edge.

static Int

Rule that aligns a child's top edge with another child's bottom edge.

static Int

Rule that centers the child horizontally with respect to the bounds of its RelativeLayout parent.

static Int

Rule that centers the child with respect to the bounds of its RelativeLayout parent.

static Int

Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent.

static Int

Rule that aligns a child's start edge with another child's end edge.

static Int

Rule that aligns a child's right edge with another child's left edge.

static Int

Rule that aligns a child's left edge with another child's right edge.

static Int

Rule that aligns a child's end edge with another child's start edge.

static Int

Inherited constants
Public constructors

RelativeLayout(context: Context!, attrs: AttributeSet!)

RelativeLayout(context: Context!, attrs: AttributeSet!, defStyleAttr: Int)

RelativeLayout(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods
open RelativeLayout.LayoutParams!

open CharSequence!

open Int

open Int

Describes how the child views are positioned.

open Int

Get the id of the View to be ignored by gravity

open Unit

open Unit
setGravity(gravity: Int)

Describes how the child views are positioned.

open Unit
setHorizontalGravity(horizontalGravity: Int)

open Unit

Defines which View is ignored when the gravity is applied.

open Unit
setVerticalGravity(verticalGravity: Int)

open Boolean

Protected methods
open Boolean

open ViewGroup.LayoutParams!

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#WRAP_CONTENT, a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT and no spanning.

open ViewGroup.LayoutParams!

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

Inherited functions
Inherited properties

XML attributes

android:gravity

android:gravity
Specifies how an object should position its content, on both the X and Y axes, within its own bounds.

Must be one or more (separated by '|') of the following constant values.

Constant Value Description
bottom 50 Push object to the bottom of its container, not changing its size.
center 11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal 1 Place object in the horizontal center of its container, not changing its size.
center_vertical 10 Place object in the vertical center of its container, not changing its size.
clip_horizontal 8 Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges.
clip_vertical 80 Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges.
end 800005 Push object to the end of its container, not changing its size.
fill 77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal 7 Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical 70 Grow the vertical size of the object if needed so it completely fills its container.
left 3 Push object to the left of its container, not changing its size.
right 5 Push object to the right of its container, not changing its size.
start 800003 Push object to the beginning of its container, not changing its size.
top 30 Push object to the top of its container, not changing its size.

android:ignoreGravity

android:ignoreGravity
Indicates what view should not be affected by gravity.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

Constants

ABOVE

Added in API level 1
static val ABOVE: Int

Rule that aligns a child's bottom edge with another child's top edge.

Value: 2

ALIGN_BASELINE

Added in API level 1
static val ALIGN_BASELINE: Int

Rule that aligns a child's baseline with another child's baseline.

Value: 4

ALIGN_BOTTOM

Added in API level 1
static val ALIGN_BOTTOM: Int

Rule that aligns a child's bottom edge with another child's bottom edge.

Value: 8

ALIGN_END

Added in API level 17
static val ALIGN_END: Int

Rule that aligns a child's end edge with another child's end edge.

Value: 19

ALIGN_LEFT

Added in API level 1
static val ALIGN_LEFT: Int

Rule that aligns a child's left edge with another child's left edge.

Value: 5

ALIGN_PARENT_BOTTOM

Added in API level 1
static val ALIGN_PARENT_BOTTOM: Int

Rule that aligns the child's bottom edge with its RelativeLayout parent's bottom edge.

Value: 12

ALIGN_PARENT_END

Added in API level 17
static val ALIGN_PARENT_END: Int

Rule that aligns the child's end edge with its RelativeLayout parent's end edge.

Value: 21

ALIGN_PARENT_LEFT

Added in API level 1
static val ALIGN_PARENT_LEFT: Int

Rule that aligns the child's left edge with its RelativeLayout parent's left edge.

Value: 9

ALIGN_PARENT_RIGHT

Added in API level 1
static val ALIGN_PARENT_RIGHT: Int

Rule that aligns the child's right edge with its RelativeLayout parent's right edge.

Value: 11

ALIGN_PARENT_START

Added in API level 17
static val ALIGN_PARENT_START: Int

Rule that aligns the child's start edge with its RelativeLayout parent's start edge.

Value: 20

ALIGN_PARENT_TOP

Added in API level 1
static val ALIGN_PARENT_TOP: Int

Rule that aligns the child's top edge with its RelativeLayout parent's top edge.

Value: 10

ALIGN_RIGHT

Added in API level 1
static val ALIGN_RIGHT: Int

Rule that aligns a child's right edge with another child's right edge.

Value: 7

ALIGN_START

Added in API level 17
static val ALIGN_START: Int

Rule that aligns a child's start edge with another child's start edge.

Value: 18

ALIGN_TOP

Added in API level 1
static val ALIGN_TOP: Int

Rule that aligns a child's top edge with another child's top edge.

Value: 6

BELOW

Added in API level 1
static val BELOW: Int

Rule that aligns a child's top edge with another child's bottom edge.

Value: 3

CENTER_HORIZONTAL

Added in API level 1
static val CENTER_HORIZONTAL: Int

Rule that centers the child horizontally with respect to the bounds of its RelativeLayout parent.

Value: 14

CENTER_IN_PARENT

Added in API level 1
static val CENTER_IN_PARENT: Int

Rule that centers the child with respect to the bounds of its RelativeLayout parent.

Value: 13

CENTER_VERTICAL

Added in API level 1
static val CENTER_VERTICAL: Int

Rule that centers the child vertically with respect to the bounds of its RelativeLayout parent.

Value: 15

END_OF

Added in API level 17
static val END_OF: Int

Rule that aligns a child's start edge with another child's end edge.

Value: 17

LEFT_OF

Added in API level 1
static val LEFT_OF: Int

Rule that aligns a child's right edge with another child's left edge.

Value: 0

RIGHT_OF

Added in API level 1
static val RIGHT_OF: Int

Rule that aligns a child's left edge with another child's right edge.

Value: 1

START_OF

Added in API level 17
static val START_OF: Int

Rule that aligns a child's end edge with another child's start edge.

Value: 16

TRUE

Added in API level 1
static val TRUE: Int
Value: -1

Public constructors

RelativeLayout

Added in API level 1
RelativeLayout(context: Context!)

RelativeLayout

Added in API level 1
RelativeLayout(
    context: Context!,
    attrs: AttributeSet!)

RelativeLayout

Added in API level 1
RelativeLayout(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int)

RelativeLayout

Added in API level 21
RelativeLayout(
    context: Context!,
    attrs: AttributeSet!,
    defStyleAttr: Int,
    defStyleRes: Int)

Public methods

generateLayoutParams

Added in API level 1
open fun generateLayoutParams(attrs: AttributeSet!): RelativeLayout.LayoutParams!
Parameters
attrs AttributeSet!: the attributes to build the layout parameters from
Return
RelativeLayout.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
open fun getAccessibilityClassName(): CharSequence!

getBaseline

Added in API level 1
open fun getBaseline(): Int
Return
Int the offset of the baseline within the widget's bounds or -1 if baseline alignment is not supported

getGravity

Added in API level 16
open fun getGravity(): Int

Describes how the child views are positioned.

Return
Int the gravity.

getIgnoreGravity

Added in API level 29
open fun getIgnoreGravity(): Int

Get the id of the View to be ignored by gravity

requestLayout

Added in API level 1
open fun requestLayout(): Unit

setGravity

Added in API level 1
open fun setGravity(gravity: Int): Unit

Describes how the child views are positioned. Defaults to Gravity.START | Gravity.TOP.

Note that since RelativeLayout considers the positioning of each child relative to one another to be significant, setting gravity will affect the positioning of all children as a single unit within the parent. This happens after children have been relatively positioned.

Parameters
gravity Int: See android.view.Gravity

setHorizontalGravity

Added in API level 1
open fun setHorizontalGravity(horizontalGravity: Int): Unit

setIgnoreGravity

Added in API level 1
open fun setIgnoreGravity(viewId: Int): Unit

Defines which View is ignored when the gravity is applied. This setting has no effect if the gravity is Gravity.START | Gravity.TOP.

Parameters
viewId Int: The id of the View to be ignored by gravity, or 0 if no View should be ignored.

See Also

setVerticalGravity

Added in API level 1
open fun setVerticalGravity(verticalGravity: Int): Unit

shouldDelayChildPressedState

Added in API level 14
open fun shouldDelayChildPressedState(): Boolean

Protected methods

checkLayoutParams

Added in API level 1
protected open fun checkLayoutParams(p: ViewGroup.LayoutParams!): Boolean

generateDefaultLayoutParams

Added in API level 1
protected open fun generateDefaultLayoutParams(): ViewGroup.LayoutParams!

Returns a set of layout parameters with a width of android.view.ViewGroup.LayoutParams#WRAP_CONTENT, a height of android.view.ViewGroup.LayoutParams#WRAP_CONTENT and no spanning.

Return
ViewGroup.LayoutParams! a set of default layout parameters or null

generateLayoutParams

Added in API level 1
protected open fun generateLayoutParams(lp: ViewGroup.LayoutParams!): ViewGroup.LayoutParams!
Parameters
p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Return
ViewGroup.LayoutParams! an instance of android.view.ViewGroup.LayoutParams or one of its descendants

onLayout

Added in API level 1
protected open fun onLayout(
    changed: Boolean,
    l: Int,
    t: Int,
    r: Int,
    b: Int
): Unit
Parameters
changed Boolean: This is a new size or position for this view
left Left position, relative to parent
top Top position, relative to parent
right Right position, relative to parent
bottom Bottom position, relative to parent

onMeasure

Added in API level 1
protected open fun onMeasure(
    widthMeasureSpec: Int,
    heightMeasureSpec: Int
): Unit
Parameters
widthMeasureSpec Int: horizontal space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.
heightMeasureSpec Int: vertical space requirements as imposed by the parent. The requirements are encoded with android.view.View.MeasureSpec.