title: getHeight and getWidth for Custom View returning 0
body: <p>I have a custom <code>View</code> to represent an ImageView with a parallax effect, adapted from the one found in the answer to this SO question: <a href="https://stackoverflow.com/questions/17207612/how-to-have-a-wider-image-scrolling-in-the-background">How to have a wider image scrolling in the background</a>. </p>
  
  <p>The <code>View</code> has a <code>setBackgroundDrawable()</code> method to set its background image that does some calculations to know the size that the bitmap should have in order to be wide enough to be scrolled with a parallax effect, if cropping is needed to keep the aspect ratio, etc.</p>
  
  <p>I'm getting an "<code>IllegalArgumentException: width and height must be &gt; 0</code>" in</p>
  
  <pre><code>(...)
  
  int width = this.getWidth();
  int height = this.getHeight();
  
  Bitmap mCurrentBackground=Bitmap.createBitmap((int) (width*FACTOR), height, Config.ARGB_8888);
  
  (...)
  </code></pre>
  
  <p>because <code>getWidth()</code> and <code>getHeight()</code> are returning zero. </p>
  
  <p>I understand that this is happening because the View doesn't know its own size yet when I call the said function, for example, in an Activity's <code>onCreate</code> method. Same thing happens if I try to set the background in <code>onStart</code> or <code>onResume</code>...</p>
  
  <p>I've managed to set the background if I call the function in <code>onWindowFocusChanged()</code>, but I still get the error sometimes (e.g. if I switch the screen orientation when an <code>EditText</code> is focused).</p>
  
  <p>Is there a way to get around this? Any kind of callback or phase in the lifecycle where the whole View was created (like the onViewCreated from the Fragment class)? I can only call my custom View's <code>setBackgroundDrawable</code> after the View has been assigned a height and a width.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I have also tried adding a <code>GlobalLayoutListener</code> to the View's <code>ViewTreeObserver</code>:</p>
  
  <pre><code>ViewTreeObserver vto = parallaxBackground.getViewTreeObserver();
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  
      @SuppressLint("NewApi")
      @Override
      public void onGlobalLayout() {
  
                      ...
  
          parallaxBackground.setBackgroundDrawable(d1);
  
              ViewTreeObserver obs = parallaxBackground.getViewTreeObserver();
  
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
                  obs.removeOnGlobalLayoutListener(this);
              } else {
                  obs.removeGlobalOnLayoutListener(this);
              }
          }
  
      });
  </code></pre>
  
  <p>but it results in the same behavior I get from setting the background in the <code>onWindowFocusChanged()</code> method. I.e. the <code>gedtWidth()</code>/<code>getHeight()</code> return zero inside the listener's <code>onGlobalLayout</code> if I switch the phone's orientation when an <code>EditText</code> is selected.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>It probably has something to do with the keyboard as I observed some behavior like the height being calculated taking the keyboard's existence into account.</p>
  
  <p>Deleting:</p>
  
  <pre><code>            if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.JELLY_BEAN) {
                  obs.removeOnGlobalLayoutListener(this);
              } else {
                  obs.removeGlobalOnLayoutListener(this);
              }
  </code></pre>
  
  <p>from my <code>onGlobalLayout</code> shown in the first EDIT seems to do the trick.</p>