I just wrote an answer for someone confused by findViewById and I realised that I have a gap in my understanding. This question is for knowledge and curiosity only.
Consider this:
button = (Button)findViewById(R.id.button);
findViewById
returns an instance of View
, which is then cast to the target class. All good so far.
To setup the view, findViewById
constructs an AttributeSet
from the parameters in the associated XML declaration which it passes to the constructor of View
.
We then cast the View
instance to Button
.
How does the AttributeSet
get passed in turn to the Button
constructor?
[EDIT]
So I was the confused one :). The whole point is that when the layout is inflated, the view hierarchy already contains an instance of the view descendant class. findViewById simply returns a reference to it. Obvious when you think about it - doh..