How Inflation Works!
Examine the following code public class MainActivity extends Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }
This is what happens when you don’t call setContentView()
Examine the following code public class MainActivity extends Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
Result when calling setContentView()
What is setContentView() public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mLayoutInflater.inflate(layoutResID, mContentParent); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } Taken from PhoneWindow.java
What is setContentView() public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mLayoutInflater.inflate(layoutResID, mContentParent); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } 1 Passing in specified layout resource to LayoutInflater.inflate()
Inflation?? Inflation happens when you instantiate an XML layout resource file into its corresponding View objects.
Let’s try explaining inflation with HTML Let's explain how Inflation works!
In the background, we have the DOM Let's explain how Inflation works! html body p DOM Tree
DOM Refresh The DOM is a tree of node objects representing an HTML document. The DOM is not actual HTML markup, it is javascript node Objects.
How to add this HTML Snippet into the ? I am some text that will be inflated Imagine this HTML is stored in a separate file called snippet.html We want to take this snippet and somehow add it into the DOM
html body p I am some text that will be inflated A naive attempt. Directly add HTML to DOM nodes.
html body p I am some text that will be inflated A naive attempt. Directly add HTML to DOM nodes. BAD!!!
Convert HTML to node I am some text that will be inflated //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div);
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div);
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div);
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div); bg-color: red
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div); bg-color: red
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div); bg-color: red #text: I am…
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div); bg-color: red #text: I am…
Convert HTML to node //create a new node var div = document.createElement("div"); //set background color of to red div.style.backgroundColor = "red"; //create a node var p = document.createElement("p"); p.innerHTML = "I am some text that will be inflated"; //find var body = document.getElementByTagName("body")[0]; //attach to div.appendChild(p); //attach to body.appendChild(div); bg-color: red #text: I am…
Inflation: Going from HTML to Node Object I am some text that will be inflated snippet.html Inflater Machine
Objects Inflation: Going from HTML to Node Object I am some text that will be inflated snippet.html HTMLHTML Inflater Machine bg-color: red #text: I am…
Bringing it all together html body p bg-color: red #text: I am…
Let's explain how Inflation works! I am some text that will be inflated html body p bg-color: red #text: I am…
((Activity)context).getLayoutInflater().inflate(R.layout.rating_text_view, this); In Android World