I want deeper understanding of how Android works, and I need someone to explain how Views are actually working "under the hood".
In normal procedure we would inflate (is this the correct word?) views from XML in onCreate method of our extended Activity with method "setContentView(R.layout.ourlayoutfile)". Then we would find Views from that XML.
Quick example: If we need to find a button we need first call "setContentVIew()" and then, "findViewById" on our button. Then we can work with this button / view respectively.
I've started to play with LayoutInflater recently, because I came to the point which I couldn't help myself with "setContentView" method, and on my surprise I found out that my Android knowledge sucks very good. I couldn't even manage LayoutInflater to work. I was embarrassed.
After a day I manage to inflate views with LayoutInflater. Actually it's not very hard, I was very close already from the start BUT there was one parameter which I didn't know what to pass in. Please look at this code: ( This is all happening in onCreate method of Activity )
View v = getLayoutInflater().inflate(R.layout.activity_main, (ViewGroup) getWindow().getDecorView());
final Button b = (Button) v.findViewById(R.id.button1);
final TextView tv = (TextView) v.findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("Just random text");
}
});
}
This code works fine, but the problem I was facing was actually this line of code:
getLayoutInflater().inflate(R.layout.activity_main, null);
I was always passing "null" parameter, and of course it didn't worked. Even tho in documentation it says that this parameter is OPTIONAL! ( Why?, if it's needed)?
I've made simple layout. Please look at it and how it looks with HiearchyViewer:
What is the second parameter in picture above, and why do we need it in there? Is maybe connecting my layout ( R.layout.activity_main ), with View provided by Android ( First view from left to right - parent view ). If that's the case, WHY doesn't android connect these two automatically?!
If there is also something useful I need to know regarding Views I will be very glad if someone could tell me ( or post a link ). Additionally would be nice if I could get some links to some websites of How Views work.. etc. Useful stuff.