Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to understand view hierarchy
    text
    copied!<p>I'm writing an app with the sole purpose of trying to understand how the view hierarchy in Android works. I am having some really harsh problems in it right now. I'll try to be concise in my explanation here.</p> <p>Setup:</p> <p>Currently I have three Views. 2 are <code>ViewGroups</code> and 1 is just a <code>View</code>. Let's say they're in this order:</p> <pre><code> TestA extends ViewGroup TestB extends ViewGroup TestC extends View TestA-&gt;TestB-&gt;TestC Where TestC is in TestB and TestB is in TestA. </code></pre> <p>In my <code>Activity</code> I simply display the views like so:</p> <pre><code>TestA myView = new TestA(context); myView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); setContentView(myView); </code></pre> <p>Problems:</p> <ol> <li><p>The <code>onDraw(Canvas canvas)</code> method of TestA is never called. I've seen a couple solutions to this saying that my view doesn't have any dimensions (height/width = 0), however, this is not the case. When I override <code>onLayout()</code>, I get the dimensions of my layout and they are correct. Also, getHeight()/Width() are exactly as they should be. I can also override <code>dispatchDraw()</code> and get my base views to draw themselves.</p></li> <li><p>I want to animate an object in <code>TestB</code>. Traditionally, I would override the <code>onDraw()</code> method on call <code>invalidate()</code> on itself until the object finish the animation it was supposed to do. However, in <code>TestB</code>, when I call <code>invalidate()</code> the view never gets redrawn. I'm under the impression that it's the job of my parent view to call the <code>onDraw()</code> method again, but my parent view does not call the <code>dispatchDraw()</code> again.</p></li> </ol> <p>I guess my questions are, why would my <code>onDraw()</code> method of my parent view never get called to begin with? What methods in my parent view are supposed to be called when one of it's children invalidate itself? Is the parent the one responsible for ensure it's children get drawn or does Android take care of that? If Android responds to <code>invalidate()</code>, why does my <code>TestB</code> never get drawn again?</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