Skip to content

Most visited

Recently visited

navigation

RelativeLayout

public class RelativeLayout
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.RelativeLayout
Known Direct Subclasses


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

class RelativeLayout.LayoutParams

Per-child layout information associated with 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

From class android.view.ViewGroup
From class android.view.View

Constants

int ABOVE

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

int ALIGN_BASELINE

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

int ALIGN_BOTTOM

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

int ALIGN_END

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

int ALIGN_LEFT

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

int ALIGN_PARENT_BOTTOM

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

int ALIGN_PARENT_END

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

int ALIGN_PARENT_LEFT

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

int ALIGN_PARENT_RIGHT

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

int ALIGN_PARENT_START

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

int ALIGN_PARENT_TOP

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

int ALIGN_RIGHT

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

int ALIGN_START

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

int ALIGN_TOP

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

int BELOW

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

int CENTER_HORIZONTAL

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

int CENTER_IN_PARENT

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

int CENTER_VERTICAL

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

int END_OF

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

int LEFT_OF

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

int RIGHT_OF

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

int START_OF

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

int TRUE

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

RelativeLayout(Context context)
RelativeLayout(Context context, AttributeSet attrs)
RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr)
RelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

RelativeLayout.LayoutParams generateLayoutParams(AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getBaseline()

Return the offset of the widget's text baseline from the widget's top boundary.

int getGravity()

Describes how the child views are positioned.

void requestLayout()

Call this when something has changed which has invalidated the layout of this view.

void setGravity(int gravity)

Describes how the child views are positioned.

void setHorizontalGravity(int horizontalGravity)
void setIgnoreGravity(int viewId)

Defines which View is ignored when the gravity is applied.

void setVerticalGravity(int verticalGravity)
boolean shouldDelayChildPressedState()

Return true if the pressed state should be delayed for children or descendants of this ViewGroup.

Protected methods

boolean checkLayoutParams(ViewGroup.LayoutParams p)
ViewGroup.LayoutParams generateDefaultLayoutParams()

Returns a set of layout parameters with a width of WRAP_CONTENT, a height of WRAP_CONTENT and no spanning.

ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)

Returns a safe set of layout parameters based on the supplied layout params.

void onLayout(boolean changed, int l, int t, int r, int b)

Called from layout when this view should assign a size and position to each of its children.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Inherited methods

From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource

XML attributes

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.

ConstantValueDescription
top0x30 Push object to the top of its container, not changing its size.
bottom0x50 Push object to the bottom of its container, not changing its size.
left0x03 Push object to the left of its container, not changing its size.
right0x05 Push object to the right of its container, not changing its size.
center_vertical0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
clip_vertical0x80 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.
clip_horizontal0x08 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.
start0x00800003 Push object to the beginning of its container, not changing its size.
end0x00800005 Push object to the end of its container, not changing its size.

This corresponds to the global attribute resource symbol gravity.

Related methods:

android:ignoreGravity

Indicates what view should not be affected by gravity.

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

This corresponds to the global attribute resource symbol ignoreGravity.

Related methods:

Constants

ABOVE

Added in API level 1
int ABOVE

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

Constant Value: 2 (0x00000002)

ALIGN_BASELINE

Added in API level 1
int ALIGN_BASELINE

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

Constant Value: 4 (0x00000004)

ALIGN_BOTTOM

Added in API level 1
int ALIGN_BOTTOM

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

Constant Value: 8 (0x00000008)

ALIGN_END

Added in API level 17
int ALIGN_END

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

Constant Value: 19 (0x00000013)

ALIGN_LEFT

Added in API level 1
int ALIGN_LEFT

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

Constant Value: 5 (0x00000005)

ALIGN_PARENT_BOTTOM

Added in API level 1
int ALIGN_PARENT_BOTTOM

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

Constant Value: 12 (0x0000000c)

ALIGN_PARENT_END

Added in API level 17
int ALIGN_PARENT_END

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

Constant Value: 21 (0x00000015)

ALIGN_PARENT_LEFT

Added in API level 1
int ALIGN_PARENT_LEFT

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

Constant Value: 9 (0x00000009)

ALIGN_PARENT_RIGHT

Added in API level 1
int ALIGN_PARENT_RIGHT

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

Constant Value: 11 (0x0000000b)

ALIGN_PARENT_START

Added in API level 17
int ALIGN_PARENT_START

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

Constant Value: 20 (0x00000014)

ALIGN_PARENT_TOP

Added in API level 1
int ALIGN_PARENT_TOP

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

Constant Value: 10 (0x0000000a)

ALIGN_RIGHT

Added in API level 1
int ALIGN_RIGHT

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

Constant Value: 7 (0x00000007)

ALIGN_START

Added in API level 17
int ALIGN_START

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

Constant Value: 18 (0x00000012)

ALIGN_TOP

Added in API level 1
int ALIGN_TOP

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

Constant Value: 6 (0x00000006)

BELOW

Added in API level 1
int BELOW

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

Constant Value: 3 (0x00000003)

CENTER_HORIZONTAL

Added in API level 1
int CENTER_HORIZONTAL

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

Constant Value: 14 (0x0000000e)

CENTER_IN_PARENT

Added in API level 1
int CENTER_IN_PARENT

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

Constant Value: 13 (0x0000000d)

CENTER_VERTICAL

Added in API level 1
int CENTER_VERTICAL

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

Constant Value: 15 (0x0000000f)

END_OF

Added in API level 17
int END_OF

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

Constant Value: 17 (0x00000011)

LEFT_OF

Added in API level 1
int LEFT_OF

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

Constant Value: 0 (0x00000000)

RIGHT_OF

Added in API level 1
int RIGHT_OF

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

Constant Value: 1 (0x00000001)

START_OF

Added in API level 17
int START_OF

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

Constant Value: 16 (0x00000010)

TRUE

Added in API level 1
int TRUE

Constant Value: -1 (0xffffffff)

Public constructors

RelativeLayout

Added in API level 1
RelativeLayout (Context context)

Parameters
context Context

RelativeLayout

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

Parameters
context Context
attrs AttributeSet

RelativeLayout

Added in API level 1
RelativeLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context
attrs AttributeSet
defStyleAttr int

RelativeLayout

Added in API level 21
RelativeLayout (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context
attrs AttributeSet
defStyleAttr int
defStyleRes int

Public methods

generateLayoutParams

Added in API level 1
RelativeLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Returns a new set of layout parameters based on the supplied attributes set.

Parameters
attrs AttributeSet: the attributes to build the layout parameters from
Returns
RelativeLayout.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getBaseline

Added in API level 1
int getBaseline ()

Return the offset of the widget's text baseline from the widget's top boundary. If this widget does not support baseline alignment, this method returns -1.

Returns
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
int getGravity ()

Describes how the child views are positioned.

Related XML Attributes:

Returns
int the gravity.

See also:

requestLayout

Added in API level 1
void requestLayout ()

Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout(). If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.

Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.

setGravity

Added in API level 1
void setGravity (int gravity)

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.

Related XML Attributes:

Parameters
gravity int: See Gravity

See also:

setHorizontalGravity

Added in API level 1
void setHorizontalGravity (int horizontalGravity)

Parameters
horizontalGravity int

setIgnoreGravity

Added in API level 1
void setIgnoreGravity (int viewId)

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

Related XML Attributes:

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
void setVerticalGravity (int verticalGravity)

Parameters
verticalGravity int

shouldDelayChildPressedState

Added in API level 14
boolean shouldDelayChildPressedState ()

Return true if the pressed state should be delayed for children or descendants of this ViewGroup. Generally, this should be done for containers that can scroll, such as a List. This prevents the pressed state from appearing when the user is actually trying to scroll the content. The default implementation returns true for compatibility reasons. Subclasses that do not scroll should generally override this method and return false.

Returns
boolean

Protected methods

checkLayoutParams

Added in API level 1
boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams
Returns
boolean

generateDefaultLayoutParams

Added in API level 1
ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns a set of layout parameters with a width of WRAP_CONTENT, a height of WRAP_CONTENT and no spanning.

Returns
ViewGroup.LayoutParams a set of default layout parameters or null

generateLayoutParams

Added in API level 1
ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams lp)

Returns a safe set of layout parameters based on the supplied layout params. When a ViewGroup is passed a View whose layout params do not pass the test of checkLayoutParams(android.view.ViewGroup.LayoutParams), this method is invoked. This method should return a new set of layout params suitable for this ViewGroup, possibly by copying the appropriate attributes from the specified set of layout params.

Parameters
lp ViewGroup.LayoutParams: The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns
ViewGroup.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

onLayout

Added in API level 1
void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.

Parameters
changed boolean: This is a new size or position for this view
l int: Left position, relative to parent
t int: Top position, relative to parent
r int: Right position, relative to parent
b int: Bottom position, relative to parent

onMeasure

Added in API level 1
void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec int: horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec int: vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.