Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what I did in my activity to figure out the content layout of a particular view child that I knew stretched end-to-end on the screen (a webview in this case):</p> <p>First, inherit from OnGlobalLayoutListener:</p> <pre><code>public class ContainerActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener { ... </code></pre> <p>Next, implement onGlobalLayout method of listener's interface (note I do some pixel calcs that pertain to figuring out the medium-zoom dip :</p> <pre><code>@Override public void onGlobalLayout() { int nH = this.mWebView.getHeight(); int nW = this.mWebView.getWidth(); if (nH &gt; 0 &amp;&amp; nW &gt; 0) { DisplayMetrics oMetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(oMetrics); nH = (nH * DisplayMetrics.DENSITY_DEFAULT) / oMetrics.densityDpi; nW = (nW * DisplayMetrics.DENSITY_DEFAULT) / oMetrics.densityDpi; // do something with nH and nW this.mWebView.getViewTreeObserver().removeGlobalOnLayoutListener ( this ); ... } } </code></pre> <p>Finally, make sure to tell the view element (mWebView in this case) inside onCreate() that you're listening to layout events:</p> <pre><code>this.setContentView(this.mWebView = new WebView(this)); this.mWebView.getViewTreeObserver().addOnGlobalLayoutListener ( this ); </code></pre> <p>This approach works for older android versions. I believe ics+ has a layout listener for each view you can bind to, and as such, would be used in a similar way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload