Added in API level 1

ViewGroup


abstract class ViewGroup : View, ViewManager, ViewParent
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup

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 android.view.ViewGroup.LayoutParams class which serves as the base class for layouts parameters.

Also see LayoutParams for layout attributes.

Here is a complete implementation of a custom ViewGroup that implements a simple android.widget.FrameLayout along with the ability to stack children in left and right gutters.

If you are implementing XML layout attributes as shown in the example, this is the corresponding definition for them that would go in res/values/attrs.xml:

Finally the layout manager can be used in an XML layout like so:

Summary

Nested classes
open

LayoutParams are used by views to tell their parents how they want to be laid out.

open

Per-child layout information for layouts that support margins.

abstract

Interface definition for a callback to be invoked when the hierarchy within this view changed.

XML attributes
android:addStatesFromChildren Sets whether this ViewGroup's drawable states also include its children's drawable states.
android:alwaysDrawnWithCache Defines whether the ViewGroup should always draw its children using their drawing cache or not.
android:animateLayoutChanges Defines whether changes in layout (caused by adding and removing items) should cause a LayoutTransition to run.
android:animationCache Defines whether layout animations should create a drawing cache for their children.
android:clipChildren Defines whether a child is limited to draw inside of its bounds or not.
android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.
android:descendantFocusability Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out.
android:layoutMode Defines the layout mode of this ViewGroup.
android:persistentDrawingCache Defines the persistence of the drawing cache.
android:splitMotionEvents Sets whether this ViewGroup should split MotionEvents to separate child views during touch event dispatch.
Inherited XML attributes
Constants
static Int

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

static Int

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

static Int

This view will get focus before any of its descendants.

static Int

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

static Int

This constant is a layoutMode.

static Int

This constant is a layoutMode.

static Int

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

static Int

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

static Int

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

static Int

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

Inherited constants
Public constructors
ViewGroup(context: Context!)

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

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

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

Public methods
open Unit

open Unit

Adds extra data to an AccessibilityNodeInfo based on an explicit request for the additional data.

open Unit
addFocusables(views: ArrayList<View!>!, direction: Int, focusableMode: Int)

open Unit

open Boolean

Returns whether this ViewGroup's drawable states also include its children's drawable states.

open Unit

open Unit
addView(child: View!)

Adds a child view.

open Unit
addView(child: View!, params: ViewGroup.LayoutParams!)

Adds a child view with the specified layout parameters.

open Unit
addView(child: View!, index: Int)

Adds a child view.

open Unit
addView(child: View!, index: Int, params: ViewGroup.LayoutParams!)

Adds a child view with the specified layout parameters.

open Unit
addView(child: View!, width: Int, height: Int)

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

open Unit

open Unit

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

open Unit
childHasTransientStateChanged(child: View, childHasTransientState: Boolean)

Called when a child view has changed whether or not it is tracking transient state.

open Unit

open Unit

Removes any pending animations for views that have been removed.

open Unit

open WindowInsets!

open Boolean

open Unit

open Unit

Dispatch to collect the ViewTranslationRequests for translation purpose by traversing the hierarchy when the app requests ui translation.

open Unit

open Boolean

open Unit

Dispatches drawable hotspot changes to child views that meet at least one of the following criteria:

open Boolean

open Boolean

open Boolean

open Unit

open Unit

Dispatches creation of a ViewStructures for autofill purposes down the hierarchy, when an Assist structure is being created as part of an autofill request.

open Unit

Dispatch creation of ViewStructure down the hierarchy.

open Unit
dispatchScrollCaptureSearch(localVisibleRect: Rect, windowOffset: Point, targets: Consumer<ScrollCaptureTarget!>)

Handle the scroll capture search request by checking this view if applicable, then to each child view.

open Unit

open Unit

open Unit

open Boolean

open Boolean

open Boolean
dispatchUnhandledMove(focused: View!, direction: Int)

open Unit

open Unit

Dispatches WindowInsetsAnimation.Callback.onEnd(WindowInsetsAnimation) when Window Insets animation ends.

open Unit

Dispatches WindowInsetsAnimation.Callback.onPrepare(WindowInsetsAnimation) when Window Insets animation is being prepared.

open WindowInsets

Dispatches WindowInsetsAnimation.Callback.onProgress(WindowInsets, List) when Window Insets animation makes progress.

open WindowInsetsAnimation.Bounds

Dispatches WindowInsetsAnimation.Callback.onStart(WindowInsetsAnimation, Bounds) when Window Insets animation is started.

open Unit

open Unit

open Unit

This method should always be called following an earlier call to startViewTransition(android.view.View).

open View!

open OnBackInvokedDispatcher?

Walk up the View hierarchy to find the nearest OnBackInvokedDispatcher.

open Unit
findViewsWithText(outViews: ArrayList<View!>!, text: CharSequence!, flags: Int)

open View!
focusSearch(focused: View!, direction: Int)

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

open Unit

open Boolean

open ViewGroup.LayoutParams!

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

open CharSequence!

open View!
getChildAt(index: Int)

Returns the view at the specified position in the group.

open Int

Returns the number of children in the group.

Int
getChildDrawingOrder(drawingPosition: Int)

Converts drawing order position to container position.

open static Int
getChildMeasureSpec(spec: Int, padding: Int, childDimension: Int)

Does the hard part of measureChildren: figuring out the MeasureSpec to pass to a particular child.

open Boolean
getChildVisibleRect(child: View!, r: Rect!, offset: Point!)

open Boolean

Returns whether this group's children are clipped to their bounds before drawing.

open Boolean

Returns whether this ViewGroup will clip its children to its padding, and resize (but not clip) any EdgeEffect to the padded region, if padding is present.

open Int

Gets the descendant focusability of this view group.

open View!

Returns the focused child of this view, if any.

open LayoutAnimationController!

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

open Animation.AnimationListener!

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

open Int

Returns the basis of alignment during layout operations on this ViewGroup: either LAYOUT_MODE_CLIP_BOUNDS or LAYOUT_MODE_OPTICAL_BOUNDS.

open LayoutTransition!

Gets the LayoutTransition object for this ViewGroup.

open Int

Return the current axes of nested scrolling for this ViewGroup.

open ViewGroupOverlay!

Returns the ViewGroupOverlay for this view group, creating it if it does not yet exist.

open Int

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

open Boolean

Check whether this ViewGroup should ignore focus requests for itself and its children.

open Boolean

Returns true if this view has or contains focus

open Boolean

open Int
indexOfChild(child: View!)

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

Unit
invalidateChild(child: View!, dirty: Rect!)

Don't call or override this method.

open ViewParent!
invalidateChildInParent(location: IntArray!, dirty: Rect!)

Don't call or override this method.

open Boolean

Indicates whether this ViewGroup will always try to draw its children using their drawing cache.

open Boolean

Indicates whether the children's drawing cache is used during a layout animation.

open Boolean

Returns whether layout calls on this container are currently being suppressed, due to an earlier call to suppressLayout(boolean).

open Boolean

Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.

open Boolean

Returns true if this ViewGroup should be considered as a single entity for removal when executing an Activity transition.

open Unit

Unit
layout(l: Int, t: Int, r: Int, b: Int)

open Unit
notifySubtreeAccessibilityStateChanged(child: View, source: View, changeType: Int)

Unit
offsetDescendantRectToMyCoords(descendant: View!, rect: Rect!)

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

Unit
offsetRectIntoDescendantCoords(descendant: View!, rect: Rect!)

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

open Unit
onDescendantInvalidated(child: View, target: View)

The target View has been invalidated, or has had a drawing property changed that requires the hierarchy to re-render.

open Boolean

Implement this method to intercept hover events before they are handled by child views.

open Boolean

Implement this method to intercept all touch screen motion events.

open Boolean
onNestedFling(target: View, velocityX: Float, velocityY: Float, consumed: Boolean)

open Boolean
onNestedPreFling(target: View, velocityX: Float, velocityY: Float)

open Boolean

React to an accessibility action delegated by a target descendant view before the target processes it.

open Unit
onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray)

open Unit
onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int)

open Unit
onNestedScrollAccepted(child: View, target: View, axes: Int)

open Boolean

Called when a child has requested sending an AccessibilityEvent and gives an opportunity to its parent to augment the event.

open PointerIcon!
onResolvePointerIcon(event: MotionEvent!, pointerIndex: Int)

open Boolean
onStartNestedScroll(child: View, target: View, nestedScrollAxes: Int)

open Unit

open Unit
onViewAdded(child: View!)

Called when a new child is added to this ViewGroup.

open Unit

Called when a child view is removed from this ViewGroup.

open Unit
propagateRequestedFrameRate(frameRate: Float, forceOverride: Boolean)

You can set the preferred frame rate for a ViewGroup and its children using a positive number or by specifying the preferred frame rate category using constants, including REQUESTED_FRAME_RATE_CATEGORY_NO_PREFERENCE, REQUESTED_FRAME_RATE_CATEGORY_LOW, REQUESTED_FRAME_RATE_CATEGORY_NORMAL, REQUESTED_FRAME_RATE_CATEGORY_HIGH.

open Unit

open Unit

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

open Unit

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.

open Unit
removeView(view: View!)

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

open Unit
removeViewAt(index: Int)

Removes the view at the specified position in the group.

open Unit

Removes a view during layout.

open Unit
removeViews(start: Int, count: Int)

Removes the specified range of views from the group.

open Unit
removeViewsInLayout(start: Int, count: Int)

Removes a range of views during layout.

open Unit
requestChildFocus(child: View!, focused: View!)

open Boolean
requestChildRectangleOnScreen(child: View, rectangle: Rect!, immediate: Boolean)

open Unit

open Boolean
requestFocus(direction: Int, previouslyFocusedRect: Rect!)

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.

open Boolean

open Unit

open Boolean

open Unit

Schedules the layout animation to be played after the next layout pass of this view group.

open Unit

Sets whether this ViewGroup's drawable states also include its children's drawable states.

open Unit

Indicates whether this ViewGroup will always try to draw its children using their drawing cache.

open Unit

Enables or disables the children's drawing cache during a layout animation.

open Unit
setClipChildren(clipChildren: Boolean)

By default, children are clipped to their bounds before drawing.

open Unit
setClipToPadding(clipToPadding: Boolean)

Sets whether this ViewGroup will clip its children to its padding and resize (but not clip) any EdgeEffect to the padded region, if padding is present.

open Unit

Set the descendant focusability of this view group.

open Unit

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

open Unit

Specifies the animation listener to which layout animation events must be sent.

open Unit
setLayoutMode(layoutMode: Int)

Sets the basis of alignment during the layout of this ViewGroup.

open Unit

Sets the LayoutTransition object for this ViewGroup.

open Unit

Enable or disable the splitting of MotionEvents to multiple children during touch event dispatch.

open Unit

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

open Unit
setPersistentDrawingCache(drawingCacheToKeep: Int)

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

open Unit

You can set the preferred frame rate for a ViewGroup using a positive number or by specifying the preferred frame rate category using constants, including REQUESTED_FRAME_RATE_CATEGORY_NO_PREFERENCE, REQUESTED_FRAME_RATE_CATEGORY_LOW, REQUESTED_FRAME_RATE_CATEGORY_NORMAL, REQUESTED_FRAME_RATE_CATEGORY_HIGH.

open Unit
setTouchscreenBlocksFocus(touchscreenBlocksFocus: Boolean)

Set whether this ViewGroup should ignore focus requests for itself and its children.

open Unit
setTransitionGroup(isTransitionGroup: Boolean)

Changes whether or not this ViewGroup should be treated as a single entity during Activity Transitions.

open Unit

Sets a WindowInsetsAnimation.Callback to be notified about animations of windows that cause insets.

open Boolean

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

open Boolean
showContextMenuForChild(originalView: View!)

open Boolean
showContextMenuForChild(originalView: View!, x: Float, y: Float)

open ActionMode!
startActionModeForChild(originalView: View!, callback: ActionMode.Callback!)

open ActionMode!
startActionModeForChild(originalView: View!, callback: ActionMode.Callback!, type: Int)

open Unit

Runs the layout animation.

open Unit

This method tells the ViewGroup that the given View object, which should have this ViewGroup as its parent, should be kept around (re-displayed when the ViewGroup draws its children) even if it is removed from its parent.

open Unit

Tells this ViewGroup to suppress all layout() calls until layout suppression is disabled with a later call to suppressLayout(false).

open Unit

Protected methods
open Boolean
addViewInLayout(child: View!, index: Int, params: ViewGroup.LayoutParams!)

Adds a view during layout.

open Boolean
addViewInLayout(child: View!, index: Int, params: ViewGroup.LayoutParams!, preventRequestLayout: Boolean)

Adds a view during layout.

open Unit
attachLayoutAnimationParameters(child: View!, params: ViewGroup.LayoutParams!, index: Int, count: Int)

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

open Unit
attachViewToParent(child: View!, index: Int, params: ViewGroup.LayoutParams!)

Attaches a view to this view group.

open Boolean

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

open Boolean

open Unit

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

open Unit
debug(depth: Int)

open Unit

Detaches all views from the parent.

open Unit

Detaches a view from its parent.

open Unit

Detaches a view from its parent.

open Unit
detachViewsFromParent(start: Int, count: Int)

Detaches a range of views from their parents.

open Unit

Called by draw to draw the child views.

open Unit

Perform dispatching of a saveHierarchyState(android.util.SparseArray) freeze()} to only this view, not to its children.

open Boolean

open Boolean

open Boolean

open Unit

open Unit

open Unit

open Unit

Perform dispatching of a restoreHierarchyState(android.util.SparseArray) to only this view, not to its children.

open Unit
dispatchVisibilityChanged(changedView: View, visibility: Int)

open Boolean
drawChild(canvas: Canvas, child: View!, drawingTime: Long)

Draw one child of this View Group.

open Unit

open ViewGroup.LayoutParams!

Returns a set of default layout parameters.

open ViewGroup.LayoutParams!

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

open Int
getChildDrawingOrder(childCount: Int, drawingPosition: Int)

Converts drawing order position to container position.

open Boolean

Sets t to be the static transformation of the child, if set, returning a boolean to indicate whether a static transform was set.

open Boolean

Indicates whether the ViewGroup is drawing its children in the order defined by getChildDrawingOrder(int,int).

open Boolean

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

open Unit
measureChild(child: View!, parentWidthMeasureSpec: Int, parentHeightMeasureSpec: Int)

Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding.

open Unit
measureChildWithMargins(child: View!, parentWidthMeasureSpec: Int, widthUsed: Int, parentHeightMeasureSpec: Int, heightUsed: Int)

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.

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

Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding.

open Unit

open IntArray!

open Unit

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

open Boolean
onRequestFocusInDescendants(direction: Int, previouslyFocusedRect: Rect!)

Look for a descendant to call android.

open Unit
removeDetachedView(child: View!, animate: Boolean)

Finishes the removal of a detached view.

open Unit

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

open Unit

Tells the ViewGroup whether to draw its children in the order defined by the method getChildDrawingOrder(int,int).

open Unit

Tells the ViewGroup to draw its children using their drawing cache.

open Unit

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

Inherited functions