public class

FrameLayout

extends ViewGroup
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

FrameLayout is designed to block out an area on the screen to display a single item. You can add multiple children to a FrameLayout, but all children are pegged to the top left of the screen. Children are drawn in a stack, with the most recently added child on top. The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are 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
Attribute Name Related Method Description
android:foreground setForeground(Drawable) Defines the drawable to draw over the content. 
android:foregroundGravity setForegroundGravity(int) Defines the gravity to apply to the foreground drawable. 
android:measureAllChildren setMeasureAllChildren(boolean) Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. 
[Expand]
Inherited XML Attributes
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
Public Constructors
FrameLayout(Context context)
FrameLayout(Context context, AttributeSet attrs)
FrameLayout(Context context, AttributeSet attrs, int defStyle)
Public Methods
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
boolean gatherTransparentRegion(Region region)
This is used by the RootView to perform an optimization when the view hierarchy contains one or several SurfaceView.
FrameLayout.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
boolean getConsiderGoneChildrenWhenMeasuring()
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring.
Drawable getForeground()
Returns the drawable used as the foreground of this FrameLayout.
void setForeground(Drawable drawable)
Supply a Drawable that is to be rendered on top of all of the child views in the frame layout.
void setForegroundGravity(int foregroundGravity)
Describes how the foreground is positioned.
void setMeasureAllChildren(boolean measureAll)
Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring.
Protected Methods
boolean checkLayoutParams(ViewGroup.LayoutParams p)
void drawableStateChanged()
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
FrameLayout.LayoutParams generateDefaultLayoutParams()
Returns a set of layout parameters with a width of FILL_PARENT, and a height of FILL_PARENT.
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
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.

void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
boolean verifyDrawable(Drawable who)
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying.
[Expand]
Inherited Methods
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:foreground

Since: API Level

Defines the drawable to draw over the content. This can be used as an overlay. The foreground drawable participates in the padding of the content if the gravity is set to fill.

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

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This corresponds to the global attribute resource symbol foreground.

Related Methods

android:foregroundGravity

Since: API Level

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

This corresponds to the global attribute resource symbol foregroundGravity.

Related Methods

android:measureAllChildren

Since: API Level

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

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol measureAllChildren.

Public Constructors

public FrameLayout (Context context)

Since: API Level 1

public FrameLayout (Context context, AttributeSet attrs)

Since: API Level 1

public FrameLayout (Context context, AttributeSet attrs, int defStyle)

Since: API Level 1

Public Methods

public void draw (Canvas canvas)

Since: API Level 1

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, do not override this method; instead, you should implement onDraw(Canvas).

Parameters
canvas The Canvas to which the View is rendered.

public boolean gatherTransparentRegion (Region region)

Since: API Level 1

This is used by the RootView to perform an optimization when the view hierarchy contains one or several SurfaceView. SurfaceView is always considered transparent, but its children are not, therefore all View objects remove themselves from the global transparent region (passed as a parameter to this function).

Parameters
region The transparent region for this ViewRoot (window).
Returns
  • Returns true if the effective visibility of the view at this point is opaque, regardless of the transparent region; returns false if it is possible for underlying windows to be seen behind the view.

public FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)

Since: API Level 1

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

Parameters
attrs the attributes to build the layout parameters from
Returns

public boolean getConsiderGoneChildrenWhenMeasuring ()

Since: API Level 1

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

public Drawable getForeground ()

Since: API Level 1

Returns the drawable used as the foreground of this FrameLayout. The foreground drawable, if non-null, is always drawn on top of the children.

Returns
  • A Drawable or null if no foreground was set.

public void setForeground (Drawable drawable)

Since: API Level 1

Supply a Drawable that is to be rendered on top of all of the child views in the frame layout. Any padding in the Drawable will be taken into account by ensuring that the children are inset to be placed inside of the padding area.

Related XML Attributes
Parameters
drawable The Drawable to be drawn on top of the children.

public void setForegroundGravity (int foregroundGravity)

Since: API Level 1

Describes how the foreground is positioned. Defaults to FILL.

Related XML Attributes
Parameters
foregroundGravity See Gravity

public void setMeasureAllChildren (boolean measureAll)

Since: API Level 1

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

Related XML Attributes
Parameters
measureAll true to consider children marked GONE, false otherwise. Default value is false.

Protected Methods

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

Since: API Level 1

protected void drawableStateChanged ()

Since: API Level 1

This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.

Be sure to call through to the superclass when overriding this function.

protected FrameLayout.LayoutParams generateDefaultLayoutParams ()

Since: API Level 1

Returns a set of layout parameters with a width of FILL_PARENT, and a height of FILL_PARENT.

Returns
  • a set of default layout parameters or null

protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

Since: API Level 1

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
p The layout parameters to convert into a suitable set of layout parameters for this ViewGroup.
Returns

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

Since: API Level 1

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 their children.

Parameters
changed 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

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Since: API Level 1

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 overriden 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 horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

Since: API Level 1

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w Current width of this view.
h Current height of this view.
oldw Old width of this view.
oldh Old height of this view.

protected boolean verifyDrawable (Drawable who)

Since: API Level 1

If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

Be sure to call through to the super class when overriding this function.

Parameters
who The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
Returns
  • boolean If true than the Drawable is being displayed in the view; else false and it is not allowed to animate.