title: Can someone explain inflate method? Deeper understanding of Android views
body: <p>I want deeper understanding of how Android works, and I need someone to explain how Views are actually working "under the hood". </p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>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 embarassed. </p>
  
  <p>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 )</p>
  
  <pre><code>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");
          }
      });
  
  }
  </code></pre>
  
  <p>This code works fine, but the problem I was facing was actually this line of code:</p>
  
  <pre><code>getLayoutInflater().inflate(R.layout.activity_main, null);
  </code></pre>
  
  <p>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) ?</p>
  
  <p>I've made simple layout. Please look at it and how it looks with HiearchyViewer:</p>
  
  <p><img src="https://i.stack.imgur.com/1rKsq.png" alt="enter image description here"></p>
  
  <p>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?!</p>
  
  <p><img src="https://i.stack.imgur.com/JW9eK.png" alt="enter image description here"></p>
  
  <p>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.</p>
  
  <p>If someone will downvote my question, please explain. Thank you very much everyone!</p>