Added in API level 1

ViewTreeObserver


class ViewTreeObserver
kotlin.Any
   ↳ android.view.ViewTreeObserver

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change.... A ViewTreeObserver should never be instantiated by applications as it is provided by the views hierarchy. Refer to android.view.View#getViewTreeObserver() for more information.

Summary

Nested classes
abstract

Interface definition for a callback to be invoked when the view tree is about to be drawn.

abstract

Interface definition for a callback to be invoked when the focus state within the view tree changes.

abstract

Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.

abstract

Interface definition for a callback to be invoked when the view tree is about to be drawn.

abstract

Interface definition for a callback to be invoked when something in the view tree has been scrolled.

abstract

Interface definition for a callback to be invoked when the touch mode changes.

abstract

Interface definition for a callback to be invoked when the view hierarchy is attached to and detached from its window.

abstract

Interface definition for a callback to be invoked when the view hierarchy's window focus state changes.

abstract

Interface definition for a callback to be invoked when the view hierarchy's window visibility changes.

Public methods
Unit

Register a callback to be invoked when the view tree is about to be drawn.

Unit

Unit

Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes

Unit

Register a callback to be invoked when the view tree is about to be drawn

Unit

Register a callback to be invoked when a view has been scrolled.

Unit

Add a listener to be notified when the tree's transformed gesture exclusion rects change.

Unit

Register a callback to be invoked when the invoked when the touch mode changes.

Unit

Register a callback to be invoked when the view hierarchy is attached to a window.

Unit

Register a callback to be invoked when the window focus state within the view tree changes.

Unit

Register a callback to be invoked when the window visibility changes.

Unit

Notifies registered listeners that the drawing pass is about to start.

Unit

Notifies registered listeners that a global layout happened.

Boolean

Notifies registered listeners that the drawing pass is about to start.

Boolean

Indicates whether this ViewTreeObserver is alive.

Unit

Adds a frame commit callback.

Unit

Remove a previously installed global layout callback

Unit

Remove a previously installed pre-draw callback.

Unit

Remove a previously installed focus change callback.

Unit

Remove a previously installed global layout callback

Unit

Remove a previously installed pre-draw callback

Unit

Remove a previously installed scroll-changed callback

Unit

Unsubscribe the given listener from gesture exclusion rect changes.

Unit

Remove a previously installed touch mode change callback

Unit

Remove a previously installed window attach callback.

Unit

Remove a previously installed window focus change callback.

Unit

Remove a previously installed window visibility callback.

Boolean

Attempts to remove the given callback from the list of pending frame complete callbacks.

Public methods

addOnDrawListener

Added in API level 16
fun addOnDrawListener(listener: ViewTreeObserver.OnDrawListener!): Unit

Register a callback to be invoked when the view tree is about to be drawn.

Note: this method cannot be invoked from android.view.ViewTreeObserver.OnDrawListener#onDraw().

Parameters
listener ViewTreeObserver.OnDrawListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnGlobalFocusChangeListener

Added in API level 1
fun addOnGlobalFocusChangeListener(listener: ViewTreeObserver.OnGlobalFocusChangeListener!): Unit

addOnGlobalLayoutListener

Added in API level 1
fun addOnGlobalLayoutListener(listener: ViewTreeObserver.OnGlobalLayoutListener!): Unit

Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes

Parameters
listener ViewTreeObserver.OnGlobalLayoutListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnPreDrawListener

Added in API level 1
fun addOnPreDrawListener(listener: ViewTreeObserver.OnPreDrawListener!): Unit

Register a callback to be invoked when the view tree is about to be drawn

Parameters
listener ViewTreeObserver.OnPreDrawListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnScrollChangedListener

Added in API level 3
fun addOnScrollChangedListener(listener: ViewTreeObserver.OnScrollChangedListener!): Unit

Register a callback to be invoked when a view has been scrolled.

Parameters
listener ViewTreeObserver.OnScrollChangedListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnSystemGestureExclusionRectsChangedListener

Added in API level 29
fun addOnSystemGestureExclusionRectsChangedListener(listener: Consumer<MutableList<Rect!>!>): Unit

Add a listener to be notified when the tree's transformed gesture exclusion rects change. This could be the result of an animation or other layout change, or a view calling View#setSystemGestureExclusionRects(List).

Parameters
listener Consumer<MutableList<Rect!>!>: listener to add This value cannot be null.

addOnTouchModeChangeListener

Added in API level 1
fun addOnTouchModeChangeListener(listener: ViewTreeObserver.OnTouchModeChangeListener!): Unit

Register a callback to be invoked when the invoked when the touch mode changes.

Parameters
listener ViewTreeObserver.OnTouchModeChangeListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnWindowAttachListener

Added in API level 18
fun addOnWindowAttachListener(listener: ViewTreeObserver.OnWindowAttachListener!): Unit

Register a callback to be invoked when the view hierarchy is attached to a window.

Parameters
listener ViewTreeObserver.OnWindowAttachListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnWindowFocusChangeListener

Added in API level 18
fun addOnWindowFocusChangeListener(listener: ViewTreeObserver.OnWindowFocusChangeListener!): Unit

Register a callback to be invoked when the window focus state within the view tree changes.

Parameters
listener ViewTreeObserver.OnWindowFocusChangeListener!: The callback to add
Exceptions
java.lang.IllegalStateException If isAlive() returns false

addOnWindowVisibilityChangeListener

Added in API level 34
fun addOnWindowVisibilityChangeListener(listener: ViewTreeObserver.OnWindowVisibilityChangeListener): Unit

Register a callback to be invoked when the window visibility changes.

Parameters
listener ViewTreeObserver.OnWindowVisibilityChangeListener: The callback to add This value cannot be null.
Exceptions
java.lang.IllegalStateException If isAlive() returns false

dispatchOnDraw

Added in API level 16
fun dispatchOnDraw(): Unit

Notifies registered listeners that the drawing pass is about to start.

dispatchOnGlobalLayout

Added in API level 1
fun dispatchOnGlobalLayout(): Unit

Notifies registered listeners that a global layout happened. This can be called manually if you are forcing a layout on a View or a hierarchy of Views that are not attached to a Window or in the GONE state.

dispatchOnPreDraw

Added in API level 1
fun dispatchOnPreDraw(): Boolean

Notifies registered listeners that the drawing pass is about to start. If a listener returns true, then the drawing pass is canceled and rescheduled. This can be called manually if you are forcing the drawing on a View or a hierarchy of Views that are not attached to a Window or in the GONE state.

Return
Boolean True if the current draw should be canceled and rescheduled, false otherwise.

isAlive

Added in API level 1
fun isAlive(): Boolean

Indicates whether this ViewTreeObserver is alive. When an observer is not alive, any call to a method (except this one) will throw an exception. If an application keeps a long-lived reference to this ViewTreeObserver, it should always check for the result of this method before calling any other method.

Return
Boolean True if this object is alive and be used, false otherwise.

registerFrameCommitCallback

Added in API level 29
fun registerFrameCommitCallback(callback: Runnable): Unit

Adds a frame commit callback. This callback will be invoked when the current rendering content has been rendered into a frame and submitted to the swap chain. The frame may not currently be visible on the display when this is invoked, but it has been submitted. This callback is useful in combination with PixelCopy to capture the current rendered content of the UI reliably. Note: Only works with hardware rendering. Does nothing otherwise.

Parameters
callback Runnable: The callback to invoke when the frame is committed. This value cannot be null.

removeGlobalOnLayoutListener

Added in API level 1
Deprecated in API level 16
fun removeGlobalOnLayoutListener(victim: ViewTreeObserver.OnGlobalLayoutListener!): Unit

Deprecated: Use #removeOnGlobalLayoutListener instead

Remove a previously installed global layout callback

Parameters
victim ViewTreeObserver.OnGlobalLayoutListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnDrawListener

Added in API level 16
fun removeOnDrawListener(victim: ViewTreeObserver.OnDrawListener!): Unit

Remove a previously installed pre-draw callback.

Note: this method cannot be invoked from android.view.ViewTreeObserver.OnDrawListener#onDraw().

Parameters
victim ViewTreeObserver.OnDrawListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnGlobalFocusChangeListener

Added in API level 1
fun removeOnGlobalFocusChangeListener(victim: ViewTreeObserver.OnGlobalFocusChangeListener!): Unit

Remove a previously installed focus change callback.

Parameters
victim ViewTreeObserver.OnGlobalFocusChangeListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnGlobalLayoutListener

Added in API level 16
fun removeOnGlobalLayoutListener(victim: ViewTreeObserver.OnGlobalLayoutListener!): Unit

Remove a previously installed global layout callback

Parameters
victim ViewTreeObserver.OnGlobalLayoutListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnPreDrawListener

Added in API level 1
fun removeOnPreDrawListener(victim: ViewTreeObserver.OnPreDrawListener!): Unit

Remove a previously installed pre-draw callback

Parameters
victim ViewTreeObserver.OnPreDrawListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnScrollChangedListener

Added in API level 3
fun removeOnScrollChangedListener(victim: ViewTreeObserver.OnScrollChangedListener!): Unit

Remove a previously installed scroll-changed callback

Parameters
victim ViewTreeObserver.OnScrollChangedListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnSystemGestureExclusionRectsChangedListener

Added in API level 29
fun removeOnSystemGestureExclusionRectsChangedListener(listener: Consumer<MutableList<Rect!>!>): Unit

Unsubscribe the given listener from gesture exclusion rect changes.

Parameters
listener Consumer<MutableList<Rect!>!>: This value cannot be null.

removeOnTouchModeChangeListener

Added in API level 1
fun removeOnTouchModeChangeListener(victim: ViewTreeObserver.OnTouchModeChangeListener!): Unit

Remove a previously installed touch mode change callback

Parameters
victim ViewTreeObserver.OnTouchModeChangeListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnWindowAttachListener

Added in API level 18
fun removeOnWindowAttachListener(victim: ViewTreeObserver.OnWindowAttachListener!): Unit

Remove a previously installed window attach callback.

Parameters
victim ViewTreeObserver.OnWindowAttachListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnWindowFocusChangeListener

Added in API level 18
fun removeOnWindowFocusChangeListener(victim: ViewTreeObserver.OnWindowFocusChangeListener!): Unit

Remove a previously installed window focus change callback.

Parameters
victim ViewTreeObserver.OnWindowFocusChangeListener!: The callback to remove
Exceptions
java.lang.IllegalStateException If isAlive() returns false

removeOnWindowVisibilityChangeListener

Added in API level 34
fun removeOnWindowVisibilityChangeListener(victim: ViewTreeObserver.OnWindowVisibilityChangeListener): Unit

Remove a previously installed window visibility callback.

Parameters
victim ViewTreeObserver.OnWindowVisibilityChangeListener: The callback to remove This value cannot be null.
Exceptions
java.lang.IllegalStateException If isAlive() returns false

unregisterFrameCommitCallback

Added in API level 29
fun unregisterFrameCommitCallback(callback: Runnable): Boolean

Attempts to remove the given callback from the list of pending frame complete callbacks.

Parameters
callback Runnable: The callback to remove This value cannot be null.
Return
Boolean Whether or not the callback was removed. If this returns true the callback will not be invoked. If false is returned then the callback was either never added or may already be pending execution and was unable to be removed