FrameLayout


public class FrameLayout
extends ViewGroup

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout


FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are View.GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.

Summary

Nested classes

class FrameLayout.LayoutParams

Per-child layout information for layouts that support margins. 

XML attributes

android:foregroundGravity Defines the gravity to apply to the foreground drawable. 
android:measureAllChildren Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

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

Public methods

FrameLayout.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.

boolean getConsiderGoneChildrenWhenMeasuring()

This method was deprecated in API level 15. This method is deprecated in favor of getMeasureAllChildren(), which was renamed for consistency with setMeasureAllChildren().

boolean getMeasureAllChildren()

Determines whether all children, or just those in the VISIBLE or INVISIBLE state, are considered when measuring.

void setForegroundGravity(int foregroundGravity)

Describes how the foreground is positioned.

void setMeasureAllChildren(boolean measureAll)

Sets whether to consider all children, or just those in the VISIBLE or INVISIBLE state, when measuring.

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)
FrameLayout.LayoutParams generateDefaultLayoutParams()

Returns a set of layout parameters with a width of ViewGroup.LayoutParams.MATCH_PARENT, and a height of ViewGroup.LayoutParams.MATCH_PARENT.

ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp)

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

void onLayout(boolean changed, int left, int top, int right, int bottom)

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

XML attributes

android:foregroundGravity

Defines the gravity to apply to the foreground drawable. The gravity defaults to fill.

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

ConstantValueDescription
bottom50Push object to the bottom of its container, not changing its size.
center11Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal1Place object in the horizontal center of its container, not changing its size.
center_vertical10Place object in the vertical center of its container, not changing its size.
clip_horizontal8Additional 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_vertical80Additional 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.
fill77Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal7Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical70Grow the vertical size of the object if needed so it completely fills its container.
left3Push object to the left of its container, not changing its size.
right5Push object to the right of its container, not changing its size.
top30Push object to the top of its container, not changing its size.

Related methods:

android:measureAllChildren

Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.

May be a boolean value, such as "true" or "false".

Related methods:

Public constructors

FrameLayout

Added in API level 1
public FrameLayout (Context context)

Parameters
context Context: This value cannot be null.

FrameLayout

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

Parameters
context Context: This value cannot be null.

attrs AttributeSet: This value may be null.

FrameLayout

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

Parameters
context Context: This value cannot be null.

attrs AttributeSet: This value may be null.

defStyleAttr int

FrameLayout

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

Parameters
context Context: This value cannot be null.

attrs AttributeSet: This value may be null.

defStyleAttr int

defStyleRes int

Public methods

generateLayoutParams

Added in API level 1
public FrameLayout.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
FrameLayout.LayoutParams an instance of ViewGroup.LayoutParams or one of its descendants

getAccessibilityClassName

Added in API level 23
public 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

getConsiderGoneChildrenWhenMeasuring

Added in API level 1
Deprecated in API level 15
public boolean getConsiderGoneChildrenWhenMeasuring ()

This method was deprecated in API level 15.
This method is deprecated in favor of getMeasureAllChildren(), which was renamed for consistency with setMeasureAllChildren().

Determines whether all children, or just those in the VISIBLE or INVISIBLE state, are considered when measuring.

Returns
boolean Whether all children are considered when measuring.

getMeasureAllChildren

Added in API level 14
public boolean getMeasureAllChildren ()

Determines whether all children, or just those in the VISIBLE or INVISIBLE state, are considered when measuring.

Returns
boolean Whether all children are considered when measuring.

setForegroundGravity

Added in API level 1
public void setForegroundGravity (int foregroundGravity)

Describes how the foreground is positioned. Defaults to START and TOP.

Related XML Attributes:

Parameters
foregroundGravity int: See Gravity

setMeasureAllChildren

Added in API level 1
public void setMeasureAllChildren (boolean measureAll)

Sets whether to consider all children, or just those in the VISIBLE or INVISIBLE state, when measuring. Defaults to false.

Related XML Attributes:

Parameters
measureAll boolean: true to consider children marked GONE, false otherwise. Default value is false.

shouldDelayChildPressedState

Added in API level 14
public 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
protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Parameters
p ViewGroup.LayoutParams

Returns
boolean

generateDefaultLayoutParams

Added in API level 1
protected FrameLayout.LayoutParams generateDefaultLayoutParams ()

Returns a set of layout parameters with a width of ViewGroup.LayoutParams.MATCH_PARENT, and a height of ViewGroup.LayoutParams.MATCH_PARENT.

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

generateLayoutParams

Added in API level 1
protected 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
protected void onLayout (boolean changed, 
                int left, 
                int top, 
                int right, 
                int bottom)

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

left int: Left position, relative to parent

top int: Top position, relative to parent

right int: Right position, relative to parent

bottom int: Bottom position, relative to parent

onMeasure

Added in API level 1
protected 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.