Most visited

Recently visited

Added in API level 1

ViewTreeObserver

public final class ViewTreeObserver
extends Object

java.lang.Object
   ↳ 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 getViewTreeObserver() for more information.

Summary

Nested classes

interface ViewTreeObserver.OnDrawListener

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

interface ViewTreeObserver.OnGlobalFocusChangeListener

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

interface ViewTreeObserver.OnGlobalLayoutListener

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

interface ViewTreeObserver.OnPreDrawListener

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

interface ViewTreeObserver.OnScrollChangedListener

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

interface ViewTreeObserver.OnTouchModeChangeListener

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

interface ViewTreeObserver.OnWindowAttachListener

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

interface ViewTreeObserver.OnWindowFocusChangeListener

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

Public methods

void addOnDrawListener(ViewTreeObserver.OnDrawListener listener)

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

void addOnGlobalFocusChangeListener(ViewTreeObserver.OnGlobalFocusChangeListener listener)

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

void addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener)

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

void addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener listener)

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

void addOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener listener)

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

void addOnTouchModeChangeListener(ViewTreeObserver.OnTouchModeChangeListener listener)

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

void addOnWindowAttachListener(ViewTreeObserver.OnWindowAttachListener listener)

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

void addOnWindowFocusChangeListener(ViewTreeObserver.OnWindowFocusChangeListener listener)

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

final void dispatchOnDraw()

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

final void dispatchOnGlobalLayout()

Notifies registered listeners that a global layout happened.

final boolean dispatchOnPreDraw()

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

boolean isAlive()

Indicates whether this ViewTreeObserver is alive.

void removeGlobalOnLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim)

This method was deprecated in API level 16. Use #removeOnGlobalLayoutListener instead

void removeOnDrawListener(ViewTreeObserver.OnDrawListener victim)

Remove a previously installed pre-draw callback.

void removeOnGlobalFocusChangeListener(ViewTreeObserver.OnGlobalFocusChangeListener victim)

Remove a previously installed focus change callback.

void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener victim)

Remove a previously installed global layout callback

void removeOnPreDrawListener(ViewTreeObserver.OnPreDrawListener victim)

Remove a previously installed pre-draw callback

void removeOnScrollChangedListener(ViewTreeObserver.OnScrollChangedListener victim)

Remove a previously installed scroll-changed callback

void removeOnTouchModeChangeListener(ViewTreeObserver.OnTouchModeChangeListener victim)

Remove a previously installed touch mode change callback

void removeOnWindowAttachListener(ViewTreeObserver.OnWindowAttachListener victim)

Remove a previously installed window attach callback.

void removeOnWindowFocusChangeListener(ViewTreeObserver.OnWindowFocusChangeListener victim)

Remove a previously installed window focus change callback.

Inherited methods

From class java.lang.Object

Public methods

addOnDrawListener

Added in API level 16
void addOnDrawListener (ViewTreeObserver.OnDrawListener listener)

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

Note: this method cannot be invoked from onDraw().

Parameters
listener ViewTreeObserver.OnDrawListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnGlobalFocusChangeListener

Added in API level 1
void addOnGlobalFocusChangeListener (ViewTreeObserver.OnGlobalFocusChangeListener listener)

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

Parameters
listener ViewTreeObserver.OnGlobalFocusChangeListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnGlobalLayoutListener

Added in API level 1
void addOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener listener)

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
Throws
IllegalStateException If isAlive() returns false

addOnPreDrawListener

Added in API level 1
void addOnPreDrawListener (ViewTreeObserver.OnPreDrawListener listener)

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

Parameters
listener ViewTreeObserver.OnPreDrawListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnScrollChangedListener

Added in API level 3
void addOnScrollChangedListener (ViewTreeObserver.OnScrollChangedListener listener)

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

Parameters
listener ViewTreeObserver.OnScrollChangedListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnTouchModeChangeListener

Added in API level 1
void addOnTouchModeChangeListener (ViewTreeObserver.OnTouchModeChangeListener listener)

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

Parameters
listener ViewTreeObserver.OnTouchModeChangeListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnWindowAttachListener

Added in API level 18
void addOnWindowAttachListener (ViewTreeObserver.OnWindowAttachListener listener)

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

Parameters
listener ViewTreeObserver.OnWindowAttachListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

addOnWindowFocusChangeListener

Added in API level 18
void addOnWindowFocusChangeListener (ViewTreeObserver.OnWindowFocusChangeListener listener)

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

Parameters
listener ViewTreeObserver.OnWindowFocusChangeListener: The callback to add
Throws
IllegalStateException If isAlive() returns false

dispatchOnDraw

Added in API level 16
void dispatchOnDraw ()

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

dispatchOnGlobalLayout

Added in API level 1
void dispatchOnGlobalLayout ()

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
boolean dispatchOnPreDraw ()

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.

Returns
boolean True if the current draw should be canceled and resceduled, false otherwise.

isAlive

Added in API level 1
boolean isAlive ()

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.

Returns
boolean True if this object is alive and be used, false otherwise.

removeGlobalOnLayoutListener

Added in API level 1
void removeGlobalOnLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)

This method was deprecated in API level 16.
Use #removeOnGlobalLayoutListener instead

Remove a previously installed global layout callback

Parameters
victim ViewTreeObserver.OnGlobalLayoutListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnDrawListener

Added in API level 16
void removeOnDrawListener (ViewTreeObserver.OnDrawListener victim)

Remove a previously installed pre-draw callback.

Note: this method cannot be invoked from onDraw().

Parameters
victim ViewTreeObserver.OnDrawListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnGlobalFocusChangeListener

Added in API level 1
void removeOnGlobalFocusChangeListener (ViewTreeObserver.OnGlobalFocusChangeListener victim)

Remove a previously installed focus change callback.

Parameters
victim ViewTreeObserver.OnGlobalFocusChangeListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnGlobalLayoutListener

Added in API level 16
void removeOnGlobalLayoutListener (ViewTreeObserver.OnGlobalLayoutListener victim)

Remove a previously installed global layout callback

Parameters
victim ViewTreeObserver.OnGlobalLayoutListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnPreDrawListener

Added in API level 1
void removeOnPreDrawListener (ViewTreeObserver.OnPreDrawListener victim)

Remove a previously installed pre-draw callback

Parameters
victim ViewTreeObserver.OnPreDrawListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnScrollChangedListener

Added in API level 3
void removeOnScrollChangedListener (ViewTreeObserver.OnScrollChangedListener victim)

Remove a previously installed scroll-changed callback

Parameters
victim ViewTreeObserver.OnScrollChangedListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnTouchModeChangeListener

Added in API level 1
void removeOnTouchModeChangeListener (ViewTreeObserver.OnTouchModeChangeListener victim)

Remove a previously installed touch mode change callback

Parameters
victim ViewTreeObserver.OnTouchModeChangeListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnWindowAttachListener

Added in API level 18
void removeOnWindowAttachListener (ViewTreeObserver.OnWindowAttachListener victim)

Remove a previously installed window attach callback.

Parameters
victim ViewTreeObserver.OnWindowAttachListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

removeOnWindowFocusChangeListener

Added in API level 18
void removeOnWindowFocusChangeListener (ViewTreeObserver.OnWindowFocusChangeListener victim)

Remove a previously installed window focus change callback.

Parameters
victim ViewTreeObserver.OnWindowFocusChangeListener: The callback to remove
Throws
IllegalStateException If isAlive() returns false

See also:

Hooray!