public abstract class

ViewGroup

extends View
implements ViewManager ViewParent
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

Also see ViewGroup.LayoutParams for layout attributes.

Summary

Nested Classes
public abstract class ViewGroup.LayoutParams LayoutParams are used by views to tell their parents how they want to be laid out. 
public abstract class ViewGroup.MarginLayoutParams Per-child layout information for layouts that support margins. 
public abstract class ViewGroup.OnHierarchyChangeListener Interface definition for a callback to be invoked when the hierarchy within this view changed. 
XML Attributes
Attribute Name Related Method Description
android:clipChildren setClipChildren(boolean) Defines whether a child is limited to draw inside of its bounds or not. 
android:clipToPadding setClipToPadding(boolean) Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. 
[Expand]
Inherited XML Attributes
From class android.view.View
Constants
int CLIP_TO_PADDING_MASK We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL are set at the same time.
int FLAG_USE_CHILD_DRAWING_ORDER When set, the drawing method will call getChildDrawingOrder(int, int) to get the index of the child to draw for that iteration.
int FOCUS_AFTER_DESCENDANTS This view will get focus only if none of its descendants want it.
int FOCUS_BEFORE_DESCENDANTS This view will get focus before any of its descendants.
int FOCUS_BLOCK_DESCENDANTS This view will block any of its descendants from getting focus, even if they are focusable.
int PERSISTENT_ALL_CACHES Used to indicate that all drawing caches should be kept in memory.
int PERSISTENT_ANIMATION_CACHE Used to indicate that the animation drawing cache should be kept in memory.
int PERSISTENT_NO_CACHE Used to indicate that no drawing cache should be kept in memory.
int PERSISTENT_SCROLLING_CACHE Used to indicate that the scrolling drawing cache should be kept in memory.
[Expand]
Inherited Constants
From class android.view.View
Public Constructors
ViewGroup(Context context)
ViewGroup(Context context, AttributeSet attrs)
ViewGroup(Context context, AttributeSet attrs, int defStyle)
Public Methods
void addFocusables(ArrayList<View> views, int direction)
Add any focusable views that are descendants of this view (possibly including this view if it is focusable itself) to views.
boolean addStatesFromChildren()
Returns whether this ViewGroup's drawable states also include its children's drawable states.
void addTouchables(ArrayList<View> views)
Add any touchable views that are descendants of this view (possibly including this view if it is touchable itself) to views.
void addView(View child, int index)
Adds a child view.
void addView(View child, int width, int height)
Adds a child view with this ViewGroup's default layout parameters and the specified width and height.
void addView(View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
void addView(View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
void addView(View child)
Adds a child view.
void bringChildToFront(View child)
Change the z order of the child so it's on top of all other children
void childDrawableStateChanged(View child)
If {link #addStatesFromChildren} is true, refreshes this group's drawable state (to include the states from its children).
void clearChildFocus(View child)
Called when a child of this parent is giving up focus
void clearDisappearingChildren()
Removes any pending animations for views that have been removed.
void clearFocus()
Called when this view wants to give up focus.
boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
boolean dispatchKeyEventPreIme(KeyEvent event)
Dispatch a key event before it is processed by any input method associated with the view hierarchy.
boolean dispatchKeyShortcutEvent(KeyEvent event)
Dispatches a key shortcut event.
void dispatchSetSelected(boolean selected)
Dispatch setSelected to all of this View's children.
boolean dispatchTouchEvent(MotionEvent ev)
Pass the touch screen motion event down to the target view, or this view if it is the target.
boolean dispatchTrackballEvent(MotionEvent event)
Pass a trackball motion event down to the focused view.
boolean dispatchUnhandledMove(View focused, int direction)
This method is the last chance for the focused view and its ancestors to respond to an arrow key.
void dispatchWindowFocusChanged(boolean hasFocus)
Called when the window containing this view gains or loses window focus.
void dispatchWindowVisibilityChanged(int visibility)
Dispatch a window visibility change down the view hierarchy.
View findFocus()
Find the view in the hierarchy rooted at this view that currently has focus.
View focusSearch(View focused, int direction)
Find the nearest view in the specified direction that wants to take focus.
void focusableViewAvailable(View v)
Tells the parent that a new focusable view has become available.
boolean gatherTransparentRegion(Region region)
This is used by the RootView to perform an optimization when the view hierarchy contains one or several SurfaceView.
ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
View getChildAt(int index)
Returns the view at the specified position in the group.
int getChildCount()
Returns the number of children in the group.
static int getChildMeasureSpec(int spec, int padding, int childDimension)
Does the hard part of measureChildren: figuring out the MeasureSpec to pass to a particular child.
boolean getChildVisibleRect(View child, Rect r, Point offset)
int getDescendantFocusability()
Gets the descendant focusability of this view group.
View getFocusedChild()
Returns the focused child of this view, if any.
LayoutAnimationController getLayoutAnimation()
Returns the layout animation controller used to animate the group's children.
Animation.AnimationListener getLayoutAnimationListener()
Returns the animation listener to which layout animation events are sent.
int getPersistentDrawingCache()
Returns an integer indicating what types of drawing caches are kept in memory.
boolean hasFocus()
Returns true if this view has or contains focus
boolean hasFocusable()
Returns true if this view is focusable or if it contains a reachable View for which hasFocusable() returns true.
int indexOfChild(View child)
Returns the position in the group of the specified child view.
final void invalidateChild(View child, Rect dirty)
Don't call or override this method.
ViewParent invalidateChildInParent(int[] location, Rect dirty)
Don't call or override this method.
boolean isAlwaysDrawnWithCacheEnabled()
Indicates whether this ViewGroup will always try to draw its children using their drawing cache.
boolean isAnimationCacheEnabled()
Indicates whether the children's drawing cache is used during a layout animation.
final void offsetDescendantRectToMyCoords(View descendant, Rect rect)
Offset a rectangle that is in a descendant's coordinate space into our coordinate space.
final void offsetRectIntoDescendantCoords(View descendant, Rect rect)
Offset a rectangle that is in our coordinate space into an ancestor's coordinate space.
boolean onInterceptTouchEvent(MotionEvent ev)
Implement this method to intercept all touch screen motion events.
void recomputeViewAttributes(View child)
Tell view hierarchy that the global view attributes need to be re-evaluated.
void removeAllViews()
Call this method to remove all child views from the ViewGroup.
void removeAllViewsInLayout()
Called by a ViewGroup subclass to remove child views from itself, when it must first know its size on screen before it can calculate how many child views it will render.
void removeView(View view)
void removeViewAt(int index)
Removes the view at the specified position in the group.
void removeViewInLayout(View view)
Removes a view during layout.
void removeViews(int start, int count)
Removes the specified range of views from the group.
void removeViewsInLayout(int start, int count)
Removes a range of views during layout.
void requestChildFocus(View child, View focused)
Called when a child of this parent wants focus
boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate)
Called when a child of this group wants a particular rectangle to be positioned onto the screen.
void requestDisallowInterceptTouchEvent(boolean disallowIntercept)
Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).
boolean requestFocus(int direction, Rect previouslyFocusedRect)
Call this to try to give focus to a specific view or to one of its descendants and give it hints about the direction and a specific rectangle that the focus is coming from. Looks for a view to give focus to respecting the setting specified by getDescendantFocusability().
void requestTransparentRegion(View child)
Called when a child wants the view hierarchy to gather and report transparent regions to the window compositor.
void scheduleLayoutAnimation()
Schedules the layout animation to be played after the next layout pass of this view group.
void setAddStatesFromChildren(boolean addsStates)
Sets whether this ViewGroup's drawable states also include its children's drawable states.
void setAlwaysDrawnWithCacheEnabled(boolean always)
Indicates whether this ViewGroup will always try to draw its children using their drawing cache.
void setAnimationCacheEnabled(boolean enabled)
Enables or disables the children's drawing cache during a layout animation.
void setClipChildren(boolean clipChildren)
By default, children are clipped to their bounds before drawing.
void setClipToPadding(boolean clipToPadding)
By default, children are clipped to the padding of the ViewGroup.
void setDescendantFocusability(int focusability)
Set the descendant focusability of this view group.
void setLayoutAnimation(LayoutAnimationController controller)
Sets the layout animation controller used to animate the group's children after the first layout.
void setLayoutAnimationListener(Animation.AnimationListener animationListener)
Specifies the animation listener to which layout animation events must be sent.
void setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener listener)
Register a callback to be invoked when a child is added to or removed from this view.
void setPadding(int left, int top, int right, int bottom)
Sets the padding.
void setPersistentDrawingCache(int drawingCacheToKeep)
Indicates what types of drawing caches should be kept in memory after they have been created.
boolean showContextMenuForChild(View originalView)
Bring up a context menu for the specified view or its ancestors.
void startLayoutAnimation()
Runs the layout animation.
void updateViewLayout(View view, ViewGroup.LayoutParams params)
Protected Methods
boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params)
Adds a view during layout.
boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout)
Adds a view during layout.
void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count)
Subclasses should override this method to set layout animation parameters on the supplied child.
void attachViewToParent(View child, int index, ViewGroup.LayoutParams params)
Attaches a view to this view group.
boolean canAnimate()
Indicates whether the view group has the ability to animate its children after the first layout.
boolean checkLayoutParams(ViewGroup.LayoutParams p)
void cleanupLayoutState(View child)
Prevents the specified child to be laid out during the next layout pass.
void debug(int depth)
Prints information about this view in the log output, with the tag VIEW_LOG_TAG.
void detachAllViewsFromParent()
Detaches all views from the parent.
void detachViewFromParent(View child)
Detaches a view from its parent.
void detachViewFromParent(int index)
Detaches a view from its parent.
void detachViewsFromParent(int start, int count)
Detaches a range of view from their parent.
void dispatchDraw(Canvas canvas)
Called by draw to draw the child views.
void dispatchFreezeSelfOnly(SparseArray<Parcelable> container)
Perform dispatching of a freeze() to only this view, not to its children.
void dispatchRestoreInstanceState(SparseArray<Parcelable> container)
Called by restoreHierarchyState(SparseArray) to retrieve the state for this view and its children.
void dispatchSaveInstanceState(SparseArray<Parcelable> container)
Called by saveHierarchyState(SparseArray) to store the state for this view and its children.
void dispatchSetPressed(boolean pressed)
Dispatch setPressed to all of this View's children.
void dispatchThawSelfOnly(SparseArray<Parcelable> container)
Perform dispatching of a thaw() to only this view, not to its children.
boolean drawChild(Canvas canvas, View child, long drawingTime)
Draw one child of this View Group.
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.
boolean fitSystemWindows(Rect insets)
Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag is set
ViewGroup.LayoutParams generateDefaultLayoutParams()
Returns a set of default layout parameters.
ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
Returns a safe set of layout parameters based on the supplied layout params.
int getChildDrawingOrder(int childCount, int i)
Returns the index of the child to draw for this iteration.
boolean getChildStaticTransformation(View child, Transformation t)
boolean isChildrenDrawnWithCacheEnabled()
Indicates whether the ViewGroup is currently drawing its children using their drawing cache.
void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding.
void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding and margins.
void measureChildren(int widthMeasureSpec, int heightMeasureSpec)
Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding.
void onAnimationEnd()
Invoked by a parent ViewGroup to notify the end of the animation currently associated with this view.
void onAnimationStart()
Invoked by a parent ViewGroup to notify the start of the animation currently associated with this view.
int[] onCreateDrawableState(int extraSpace)
Generate the new Drawable state for this view.
abstract 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.
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
Look for a descendant to call requestFocus() on.
void removeDetachedView(View child, boolean animate)
Finishes the removal of a detached view.
void setChildrenDrawingCacheEnabled(boolean enabled)
Enables or disables the drawing cache for each child of this view group.
void setChildrenDrawnWithCacheEnabled(boolean enabled)
Tells the ViewGroup to draw its children using their drawing cache.
void setStaticTransformationsEnabled(boolean enabled)
When this property is set to true, this ViewGroup supports static transformations on children; this causes getChildStaticTransformation(View, android.view.animation.Transformation) to be invoked when a child is drawn.
[Expand]
Inherited Methods
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

XML Attributes

android:clipChildren

Defines whether a child is limited to draw inside of its bounds or not. This is useful with animations that scale the size of the children to more than 100% for instance. In such a case, this property should be set to false to allow the children to draw outside of their bounds. The default value of this property is true.

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

Related Methods

android:clipToPadding

Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. This property is set to true by default.

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

Related Methods

Constants

protected static final int CLIP_TO_PADDING_MASK

We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL are set at the same time.

Constant Value: 34 (0x00000022)

protected static final int FLAG_USE_CHILD_DRAWING_ORDER

When set, the drawing method will call getChildDrawingOrder(int, int) to get the index of the child to draw for that iteration.

Constant Value: 1024 (0x00000400)

public static final int FOCUS_AFTER_DESCENDANTS

This view will get focus only if none of its descendants want it.

Constant Value: 262144 (0x00040000)

public static final int FOCUS_BEFORE_DESCENDANTS

This view will get focus before any of its descendants.

Constant Value: 131072 (0x00020000)

public static final int FOCUS_BLOCK_DESCENDANTS

This view will block any of its descendants from getting focus, even if they are focusable.

Constant Value: 393216 (0x00060000)

public static final int PERSISTENT_ALL_CACHES

Used to indicate that all drawing caches should be kept in memory.

Constant Value: 3 (0x00000003)

public static final int PERSISTENT_ANIMATION_CACHE

Used to indicate that the animation drawing cache should be kept in memory.

Constant Value: 1 (0x00000001)

public static final int PERSISTENT_NO_CACHE

Used to indicate that no drawing cache should be kept in memory.

Constant Value: 0 (0x00000000)

public static final int PERSISTENT_SCROLLING_CACHE

Used to indicate that the scrolling drawing cache should be kept in memory.

Constant Value: 2 (0x00000002)

Public Constructors

public ViewGroup (Context context)

public ViewGroup (Context context, AttributeSet attrs)

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

Public Methods

public void addFocusables (ArrayList<View> views, int direction)

Add any focusable views that are descendants of this view (possibly including this view if it is focusable itself) to views. If we are in touch mode, only add views that are also focusable in touch mode.

Parameters
views Focusable views found so far
direction The direction of the focus

public boolean addStatesFromChildren ()

Returns whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.

public void addTouchables (ArrayList<View> views)

Add any touchable views that are descendants of this view (possibly including this view if it is touchable itself) to views.

Parameters
views Touchable views found so far

public void addView (View child, int index)

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Parameters
child the child view to add
index the position at which to add the child

public void addView (View child, int width, int height)

Adds a child view with this ViewGroup's default layout parameters and the specified width and height.

Parameters
child the child view to add

public void addView (View child, ViewGroup.LayoutParams params)

Adds a child view with the specified layout parameters.

Parameters
child the child view to add
params the layout parameters to set on the child

public void addView (View child, int index, ViewGroup.LayoutParams params)

Adds a child view with the specified layout parameters.

Parameters
child the child view to add
index the position at which to add the child
params the layout parameters to set on the child

public void addView (View child)

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Parameters
child the child view to add

public void bringChildToFront (View child)

Change the z order of the child so it's on top of all other children

public void childDrawableStateChanged (View child)

If {link #addStatesFromChildren} is true, refreshes this group's drawable state (to include the states from its children).

Parameters
child The child whose drawable state has changed.

public void clearChildFocus (View child)

Called when a child of this parent is giving up focus

Parameters
child The view that is giving up focus

public void clearDisappearingChildren ()

Removes any pending animations for views that have been removed. Call this if you don't want animations for exiting views to stack up.

public void clearFocus ()

Called when this view wants to give up focus. This will cause onFocusChanged(boolean, int, Rect) to be called.

public boolean dispatchKeyEvent (KeyEvent event)

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled, false otherwise.

public boolean dispatchKeyEventPreIme (KeyEvent event)

Dispatch a key event before it is processed by any input method associated with the view hierarchy. This can be used to intercept key events in special situations before the IME consumes them; a typical example would be handling the BACK key to update the application's UI instead of allowing the IME to see it and close itself.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled, false otherwise.

public boolean dispatchKeyShortcutEvent (KeyEvent event)

Dispatches a key shortcut event.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public void dispatchSetSelected (boolean selected)

Dispatch setSelected to all of this View's children.

Parameters
selected The new selected state

public boolean dispatchTouchEvent (MotionEvent ev)

Pass the touch screen motion event down to the target view, or this view if it is the target.

Parameters
ev The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public boolean dispatchTrackballEvent (MotionEvent event)

Pass a trackball motion event down to the focused view.

Parameters
event The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public boolean dispatchUnhandledMove (View focused, int direction)

This method is the last chance for the focused view and its ancestors to respond to an arrow key. This is called when the focused view did not consume the key internally, nor could the view system find a new view in the requested direction to give focus to.

Parameters
focused The currently focused view.
direction The direction focus wants to move. One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
Returns
  • True if the this view consumed this unhandled move.

public void dispatchWindowFocusChanged (boolean hasFocus)

Called when the window containing this view gains or loses window focus. ViewGroups should override to route to their children.

Parameters
hasFocus True if the window containing this view now has focus, false otherwise.

public void dispatchWindowVisibilityChanged (int visibility)

Dispatch a window visibility change down the view hierarchy. ViewGroups should override to route to their children.

Parameters
visibility The new visibility of the window.

public View findFocus ()

Find the view in the hierarchy rooted at this view that currently has focus.

Returns
  • The view that currently has focus, or null if no focused view can be found.

public View focusSearch (View focused, int direction)

Find the nearest view in the specified direction that wants to take focus.

Parameters
focused The view that currently has focus
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT, or 0 for not applicable.

public void focusableViewAvailable (View v)

Tells the parent that a new focusable view has become available. This is to handle transitions from the case where there are no focusable views to the case where the first focusable view appears.

Parameters
v The view that has become newly focusable

public boolean gatherTransparentRegion (Region region)

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 ViewGroup.LayoutParams generateLayoutParams (AttributeSet attrs)

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 View getChildAt (int index)

Returns the view at the specified position in the group.

Parameters
index the position at which to get the view from
Returns
  • the view at the specified position or null if the position does not exist within the group

public int getChildCount ()

Returns the number of children in the group.

Returns
  • a positive integer representing the number of children in the group

public static int getChildMeasureSpec (int spec, int padding, int childDimension)

Does the hard part of measureChildren: figuring out the MeasureSpec to pass to a particular child. This method figures out the right MeasureSpec for one dimension (height or width) of one child view. The goal is to combine information from our MeasureSpec with the LayoutParams of the child to get the best possible results. For example, if the this view knows its size (because its MeasureSpec has a mode of EXACTLY), and the child has indicated in its LayoutParams that it wants to be the same size as the parent, the parent should ask the child to layout given an exact size.

Parameters
spec The requirements for this view
padding The padding of this view for the current dimension and margins, if applicable
childDimension How big the child wants to be in the current dimension
Returns
  • a MeasureSpec integer for the child

public boolean getChildVisibleRect (View child, Rect r, Point offset)

public int getDescendantFocusability ()

Gets the descendant focusability of this view group. The descendant focusability defines the relationship between this view group and its descendants when looking for a view to take focus in requestFocus(int, android.graphics.Rect).

public View getFocusedChild ()

Returns the focused child of this view, if any. The child may have focus or contain focus.

Returns
  • the focused child or null.

public LayoutAnimationController getLayoutAnimation ()

Returns the layout animation controller used to animate the group's children.

Returns
  • the current animation controller

public Animation.AnimationListener getLayoutAnimationListener ()

Returns the animation listener to which layout animation events are sent.

public int getPersistentDrawingCache ()

Returns an integer indicating what types of drawing caches are kept in memory.

public boolean hasFocus ()

Returns true if this view has or contains focus

Returns
  • true if this view has or contains focus

public boolean hasFocusable ()

Returns true if this view is focusable or if it contains a reachable View for which hasFocusable() returns true. A "reachable hasFocusable()" is a View whose parents do not block descendants focus. Only VISIBLE views are considered focusable.

Returns
  • True if the view is focusable or if the view contains a focusable View, false otherwise.

public int indexOfChild (View child)

Returns the position in the group of the specified child view.

Parameters
child the view for which to get the position
Returns
  • a positive integer representing the position of the view in the group, or -1 if the view does not exist in the group

public final void invalidateChild (View child, Rect dirty)

Don't call or override this method. It is used for the implementation of the view hierarchy.

Parameters
child The child which is dirty
dirty The area within the child that is invalid

public ViewParent invalidateChildInParent (int[] location, Rect dirty)

Don't call or override this method. It is used for the implementation of the view hierarchy. This implementation returns null if this ViewGroup does not have a parent, if this ViewGroup is already fully invalidated or if the dirty rectangle does not intersect with this ViewGroup's bounds.

Parameters
location An array of 2 ints containing the left and top coordinates of the child to invalidate
dirty The area within the child that is invalid
Returns
  • the parent of this ViewParent or null

public boolean isAlwaysDrawnWithCacheEnabled ()

Indicates whether this ViewGroup will always try to draw its children using their drawing cache. By default this property is enabled.

Returns
  • true if the animation cache is enabled, false otherwise

public boolean isAnimationCacheEnabled ()

Indicates whether the children's drawing cache is used during a layout animation. By default, the drawing cache is enabled but this will prevent nested layout animations from working. To nest animations, you must disable the cache.

Returns
  • true if the animation cache is enabled, false otherwise

public final void offsetDescendantRectToMyCoords (View descendant, Rect rect)

Offset a rectangle that is in a descendant's coordinate space into our coordinate space.

Parameters
descendant A descendant of this view
rect A rectangle defined in descendant's coordinate space.

public final void offsetRectIntoDescendantCoords (View descendant, Rect rect)

Offset a rectangle that is in our coordinate space into an ancestor's coordinate space.

Parameters
descendant A descendant of this view
rect A rectangle defined in descendant's coordinate space.

public boolean onInterceptTouchEvent (MotionEvent ev)

Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.

Using this function takes some care, as it has a fairly complicated interaction with View.onTouchEvent(MotionEvent), and using it requires implementing that method as well as this one in the correct way. Events will be received in the following order:

  1. You will receive the down event here.
  2. The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
  3. For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
  4. If you return true from here, you will not receive any following events: the target view will receive the same event but with the action ACTION_CANCEL, and all further events will be delivered to your onTouchEvent() method and no longer appear here.

Parameters
ev The motion event being dispatched down the hierarchy.
Returns
  • Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here.

public void recomputeViewAttributes (View child)

Tell view hierarchy that the global view attributes need to be re-evaluated.

Parameters
child View whose attributes have changed.

public void removeAllViews ()

Call this method to remove all child views from the ViewGroup.

public void removeAllViewsInLayout ()

Called by a ViewGroup subclass to remove child views from itself, when it must first know its size on screen before it can calculate how many child views it will render. An example is a Gallery or a ListView, which may "have" 50 children, but actually only render the number of children that can currently fit inside the object on screen. Do not call this method unless you are extending ViewGroup and understand the view measuring and layout pipeline.

public void removeView (View view)

public void removeViewAt (int index)

Removes the view at the specified position in the group.

Parameters
index the position in the group of the view to remove

public void removeViewInLayout (View view)

Removes a view during layout. This is useful if in your onLayout() method, you need to remove more views.

Parameters
view the view to remove from the group

public void removeViews (int start, int count)

Removes the specified range of views from the group.

Parameters
start the first position in the group of the range of views to remove
count the number of views to remove

public void removeViewsInLayout (int start, int count)

Removes a range of views during layout. This is useful if in your onLayout() method, you need to remove more views.

Parameters
start the index of the first view to remove from the group
count the number of views to remove from the group

public void requestChildFocus (View child, View focused)

Called when a child of this parent wants focus

Parameters
child The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus.
focused The view that is a descendant of child that actually has focus

public boolean requestChildRectangleOnScreen (View child, Rect rectangle, boolean immediate)

Called when a child of this group wants a particular rectangle to be positioned onto the screen. ViewGroups overriding this can trust that:

  • child will be a direct child of this group
  • rectangle will be in the child's coordinates

ViewGroups overriding this should uphold the contract:

  • nothing will change if the rectangle is already visible
  • the view port will be scrolled only just enough to make the rectangle visible
Parameters
child The direct child making the request.
rectangle The rectangle in the child's coordinates the child wishes to be on the screen.
immediate True to forbid animated or delayed scrolling, false otherwise
Returns
  • Whether the group scrolled to handle the operation

public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)

Called when a child does not want this parent and its ancestors to intercept touch events with onInterceptTouchEvent(MotionEvent).

This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.

Parameters
disallowIntercept True if the child does not want the parent to intercept touch events.

public boolean requestFocus (int direction, Rect previouslyFocusedRect)

Call this to try to give focus to a specific view or to one of its descendants and give it hints about the direction and a specific rectangle that the focus is coming from. The rectangle can help give larger views a finer grained hint about where focus is coming from, and therefore, where to show selection, or forward focus change internally. A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode. A View will not take focus if it is not visible. A View will not take focus if one of its parents has getDescendantFocusability() equal to FOCUS_BLOCK_DESCENDANTS. See also focusSearch(int), which is what you call to say that you have focus, and you want your parent to look for the next one. You may wish to override this method if your custom View has an internal View that it wishes to forward the request to. Looks for a view to give focus to respecting the setting specified by getDescendantFocusability(). Uses onRequestFocusInDescendants(int, android.graphics.Rect) to find focus within the children of this group when appropriate.

Parameters
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Returns
  • Whether this view or one of its descendants actually took focus.

public void requestTransparentRegion (View child)

Called when a child wants the view hierarchy to gather and report transparent regions to the window compositor. Views that "punch" holes in the view hierarchy, such as SurfaceView can use this API to improve performance of the system. When no such a view is present in the hierarchy, this optimization in unnecessary and might slightly reduce the view hierarchy performance.

Parameters
child the view requesting the transparent region computation

public void scheduleLayoutAnimation ()

Schedules the layout animation to be played after the next layout pass of this view group. This can be used to restart the layout animation when the content of the view group changes or when the activity is paused and resumed.

public void setAddStatesFromChildren (boolean addsStates)

Sets whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.

public void setAlwaysDrawnWithCacheEnabled (boolean always)

Indicates whether this ViewGroup will always try to draw its children using their drawing cache. This property can be set to true when the cache rendering is slightly different from the children's normal rendering. Renderings can be different, for instance, when the cache's quality is set to low. When this property is disabled, the ViewGroup will use the drawing cache of its children only when asked to. It's usually the task of subclasses to tell ViewGroup when to start using the drawing cache and when to stop using it.

Parameters
always true to always draw with the drawing cache, false otherwise

public void setAnimationCacheEnabled (boolean enabled)

Enables or disables the children's drawing cache during a layout animation. By default, the drawing cache is enabled but this will prevent nested layout animations from working. To nest animations, you must disable the cache.

Parameters
enabled true to enable the animation cache, false otherwise

public void setClipChildren (boolean clipChildren)

By default, children are clipped to their bounds before drawing. This allows view groups to override this behavior for animations, etc.

Related XML Attributes
Parameters
clipChildren true to clip children to their bounds, false otherwise

public void setClipToPadding (boolean clipToPadding)

By default, children are clipped to the padding of the ViewGroup. This allows view groups to override this behavior

Related XML Attributes
Parameters
clipToPadding true to clip children to the padding of the group, false otherwise

public void setDescendantFocusability (int focusability)

Set the descendant focusability of this view group. This defines the relationship between this view group and its descendants when looking for a view to take focus in requestFocus(int, android.graphics.Rect).

public void setLayoutAnimation (LayoutAnimationController controller)

Sets the layout animation controller used to animate the group's children after the first layout.

Parameters
controller the animation controller

public void setLayoutAnimationListener (Animation.AnimationListener animationListener)

Specifies the animation listener to which layout animation events must be sent. Only onAnimationStart(Animation) and onAnimationEnd(Animation) are invoked.

Parameters
animationListener the layout animation listener

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

Register a callback to be invoked when a child is added to or removed from this view.

Parameters
listener the callback to invoke on hierarchy change

public void setPadding (int left, int top, int right, int bottom)

Sets the padding. The view may add on the space required to display the scrollbars, depending on the style and visibility of the scrollbars. So the values returned from getPaddingLeft(), getPaddingTop(), getPaddingRight() and getPaddingBottom() may be different from the values set in this call.

Parameters
left the left padding in pixels
top the top padding in pixels
right the right padding in pixels
bottom the bottom padding in pixels

public void setPersistentDrawingCache (int drawingCacheToKeep)

Indicates what types of drawing caches should be kept in memory after they have been created.

Parameters
drawingCacheToKeep one or a combination of PERSISTENT_NO_CACHE, PERSISTENT_ANIMATION_CACHE, PERSISTENT_SCROLLING_CACHE and PERSISTENT_ALL_CACHES

public boolean showContextMenuForChild (View originalView)

Bring up a context menu for the specified view or its ancestors.

In most cases, a subclass does not need to override this. However, if the subclass is added directly to the window manager (for example, addView(View, android.view.ViewGroup.LayoutParams)) then it should override this and show the context menu.

Parameters
originalView The source view where the context menu was first invoked
Returns
  • true if a context menu was displayed

public void startLayoutAnimation ()

Runs the layout animation. Calling this method triggers a relayout of this view group.

public void updateViewLayout (View view, ViewGroup.LayoutParams params)

Protected Methods

protected boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params)

Adds a view during layout. This is useful if in your onLayout() method, you need to add more views (as does the list view for example). If index is negative, it means put it at the end of the list.

Parameters
child the view to add to the group
index the index at which the child must be added
params the layout parameters to associate with the child
Returns
  • true if the child was added, false otherwise

protected boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout)

Adds a view during layout. This is useful if in your onLayout() method, you need to add more views (as does the list view for example). If index is negative, it means put it at the end of the list.

Parameters
child the view to add to the group
index the index at which the child must be added
params the layout parameters to associate with the child
preventRequestLayout if true, calling this method will not trigger a layout request on child
Returns
  • true if the child was added, false otherwise

protected void attachLayoutAnimationParameters (View child, ViewGroup.LayoutParams params, int index, int count)

Subclasses should override this method to set layout animation parameters on the supplied child.

Parameters
child the child to associate with animation parameters
params the child's layout parameters which hold the animation parameters
index the index of the child in the view group
count the number of children in the view group

protected void attachViewToParent (View child, int index, ViewGroup.LayoutParams params)

Attaches a view to this view group. Attaching a view assigns this group as the parent, sets the layout parameters and puts the view in the list of children so it can be retrieved by calling getChildAt(int). This method should be called only for view which were detached from their parent.

Parameters
child the child to attach
index the index at which the child should be attached
params the layout parameters of the child

protected boolean canAnimate ()

Indicates whether the view group has the ability to animate its children after the first layout.

Returns
  • true if the children can be animated, false otherwise

protected boolean checkLayoutParams (ViewGroup.LayoutParams p)

protected void cleanupLayoutState (View child)

Prevents the specified child to be laid out during the next layout pass.

Parameters
child the child on which to perform the cleanup

protected void debug (int depth)

Prints information about this view in the log output, with the tag VIEW_LOG_TAG. Each line in the output is preceded with an indentation defined by the depth.

Parameters
depth the indentation level

protected void detachAllViewsFromParent ()

Detaches all views from the parent. Detaching a view should be temporary and followed either by a call to attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) or a call to removeDetachedView(View, boolean). When a view is detached, its parent is null and cannot be retrieved by a call to getChildAt(int).

protected void detachViewFromParent (View child)

Detaches a view from its parent. Detaching a view should be temporary and followed either by a call to attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) or a call to removeDetachedView(View, boolean). When a view is detached, its parent is null and cannot be retrieved by a call to getChildAt(int).

Parameters
child the child to detach

protected void detachViewFromParent (int index)

Detaches a view from its parent. Detaching a view should be temporary and followed either by a call to attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) or a call to removeDetachedView(View, boolean). When a view is detached, its parent is null and cannot be retrieved by a call to getChildAt(int).

Parameters
index the index of the child to detach

protected void detachViewsFromParent (int start, int count)

Detaches a range of view from their parent. Detaching a view should be temporary and followed either by a call to attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) or a call to removeDetachedView(View, boolean). When a view is detached, its parent is null and cannot be retrieved by a call to getChildAt(int).

Parameters
start the first index of the childrend range to detach
count the number of children to detach

protected void dispatchDraw (Canvas canvas)

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters
canvas the canvas on which to draw the view

protected void dispatchFreezeSelfOnly (SparseArray<Parcelable> container)

Perform dispatching of a freeze() to only this view, not to its children. For use when overriding dispatchFreeze() to allow subclasses to freeze their own state but not the state of their children.

Parameters
container the container

protected void dispatchRestoreInstanceState (SparseArray<Parcelable> container)

Called by restoreHierarchyState(SparseArray) to retrieve the state for this view and its children. May be overridden to modify how restoreing happens to a view's children; for example, some views may want to not store state for their children.

Parameters
container The SparseArray which holds previously saved state.

protected void dispatchSaveInstanceState (SparseArray<Parcelable> container)

Called by saveHierarchyState(SparseArray) to store the state for this view and its children. May be overridden to modify how freezing happens to a view's children; for example, some views may want to not store state for their children.

Parameters
container The SparseArray in which to save the view's state.

protected void dispatchSetPressed (boolean pressed)

Dispatch setPressed to all of this View's children.

Parameters
pressed The new pressed state

protected void dispatchThawSelfOnly (SparseArray<Parcelable> container)

Perform dispatching of a thaw() to only this view, not to its children. For use when overriding dispatchThaw() to allow subclasses to thaw their own state but not the state of their children.

Parameters
container the container

protected boolean drawChild (Canvas canvas, View child, long drawingTime)

Draw one child of this View Group. This method is responsible for getting the canvas in the right state. This includes clipping, translating so that the child's scrolled origin is at 0, 0, and applying any animation transformations.

Parameters
canvas The canvas on which to draw the child
child Who to draw
drawingTime The time at which draw is occuring
Returns
  • True if an invalidate() was issued

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

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

protected boolean fitSystemWindows (Rect insets)

Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag is set

Parameters
insets Insets for system windows
Returns
  • True if this view applied the insets, false otherwise

protected ViewGroup.LayoutParams generateDefaultLayoutParams ()

Returns a set of default layout parameters. These parameters are requested when the View passed to addView(View) has no layout parameters already set. If null is returned, an exception is thrown from addView.

Returns
  • a set of default layout parameters or null

protected ViewGroup.LayoutParams generateLayoutParams (ViewGroup.LayoutParams p)

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 int getChildDrawingOrder (int childCount, int i)

Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.

NOTE: In order for this method to be called, the FLAG_USE_CHILD_DRAWING_ORDER must be set.

Parameters
i The current iteration.
Returns
  • The index of the child to draw this iteration.

protected boolean getChildStaticTransformation (View child, Transformation t)

protected boolean isChildrenDrawnWithCacheEnabled ()

Indicates whether the ViewGroup is currently drawing its children using their drawing cache.

Returns
  • true if children should be drawn with their cache, false otherwise

protected void measureChild (View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)

Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding. The heavy lifting is done in getChildMeasureSpec.

Parameters
child The child to measure
parentWidthMeasureSpec The width requirements for this view
parentHeightMeasureSpec The height requirements for this view

protected void measureChildWithMargins (View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding and margins. The child must have MarginLayoutParams The heavy lifting is done in getChildMeasureSpec.

Parameters
child The child to measure
parentWidthMeasureSpec The width requirements for this view
widthUsed Extra space that has been used up by the parent horizontally (possibly by other children of the parent)
parentHeightMeasureSpec The height requirements for this view
heightUsed Extra space that has been used up by the parent vertically (possibly by other children of the parent)

protected void measureChildren (int widthMeasureSpec, int heightMeasureSpec)

Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding. We skip children that are in the GONE state The heavy lifting is done in getChildMeasureSpec.

Parameters
widthMeasureSpec The width requirements for this view
heightMeasureSpec The height requirements for this view

protected void onAnimationEnd ()

Invoked by a parent ViewGroup to notify the end of the animation currently associated with this view. If you override this method, always call super.onAnimationEnd();

protected void onAnimationStart ()

Invoked by a parent ViewGroup to notify the start of the animation currently associated with this view. If you override this method, always call super.onAnimationStart();

protected int[] onCreateDrawableState (int extraSpace)

Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use getDrawableState().

Parameters
extraSpace if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.
Returns
  • Returns an array holding the current Drawable state of the view.

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

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

protected boolean onRequestFocusInDescendants (int direction, Rect previouslyFocusedRect)

Look for a descendant to call requestFocus() on. Called by requestFocus(int, android.graphics.Rect) when it wants to request focus within its children. Override this to customize how your ViewGroup requests focus within its children.

Parameters
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Returns
  • Whether focus was taken.

protected void removeDetachedView (View child, boolean animate)

Finishes the removal of a detached view. This method will dispatch the detached from window event and notify the hierarchy change listener.

Parameters
child the child to be definitely removed from the view hierarchy
animate if true and the view has an animation, the view is placed in the disappearing views list, otherwise, it is detached from the window

protected void setChildrenDrawingCacheEnabled (boolean enabled)

Enables or disables the drawing cache for each child of this view group.

Parameters
enabled true to enable the cache, false to dispose of it

protected void setChildrenDrawnWithCacheEnabled (boolean enabled)

Tells the ViewGroup to draw its children using their drawing cache. This property is ignored when isAlwaysDrawnWithCacheEnabled() is true. A child's drawing cache will be used only if it has been enabled. Subclasses should call this method to start and stop using the drawing cache when they perform performance sensitive operations, like scrolling or animating.

Parameters
enabled true if children should be drawn with their cache, false otherwise

protected void setStaticTransformationsEnabled (boolean enabled)

When this property is set to true, this ViewGroup supports static transformations on children; this causes getChildStaticTransformation(View, android.view.animation.Transformation) to be invoked when a child is drawn. Any subclass overriding getChildStaticTransformation(View, android.view.animation.Transformation) should set this property to true.

Parameters
enabled True to enable static transformations on children, false otherwise.