Stats

72 Contributors: 2 Friday, October 21, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Getting Calculated View Dimensions

Remarks

Note that a ViewTreeObserver instance associated with a View instance can become invalid while that View is still alive. From the View.getViewTreeObserver javadocs:

// The returned ViewTreeObserver observer is not guaranteed to remain
// valid for the lifetime of this View. If the caller of this method keeps
// a long-lived reference to ViewTreeObserver, it should always check for
// the return value of {@link ViewTreeObserver#isAlive()}.

Thus, if you have previously added a listener to a ViewTreeObserver instance and now wish to remove it, it is easiest to call getViewTreeObserver on the corresponding View instance again to receive a fresh ViewTreeObserver instance. (Checking isAlive on an existing instance is more work for little benefit; if the ViewTreeObserver is no longer alive, you'll be fetching that fresh reference anyway!)

Related Examples