How Inflation Works!. Examine the following code public class MainActivity extends Activity {...

27
How Inflation Works!

Transcript of How Inflation Works!. Examine the following code public class MainActivity extends Activity {...

Page 1: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

How Inflation Works!

Page 2: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Examine the following codepublic class MainActivity extends Activity {

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);}

}

Page 3: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

This is what happens when you don’t call setContentView()

Page 4: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Examine the following codepublic class MainActivity extends Activity {

@Overridepublic void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);

}}

Page 5: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Result when calling setContentView()

Page 6: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

What is setContentView() doing? @Override 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

Page 7: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

What is setContentView() doing? @Override 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()

Page 8: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Inflation??

Inflation happens when you instantiate an XML layout resource

file into its corresponding View objects.

Page 9: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Let’s try explaining inflation with HTML

<html> <body> <p>Let's explain how Inflation works!</p> </body></html>

Page 10: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

In the background, we have the DOM

<html> <body> <p>Let's explain how Inflation works!</p> </body></html>

html

body

p

DOM Tree

Page 11: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

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.

Page 12: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

How to add this HTML Snippet into the <body>?

<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

• 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

Page 13: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

html

body

p<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

A naive attempt. Directly add HTML to DOM nodes.

Page 14: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

html

body

p<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

A naive attempt. Directly add HTML to DOM nodes.

BAD!!!

Page 15: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

<script type="text/javascript">//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

Page 16: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

Page 17: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>

Page 18: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>bg-color: red

Page 19: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>bg-color: red

<p>

Page 20: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>bg-color: red

<p>#text: I am…

Page 21: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>bg-color: red

<p>#text: I am…

<body>

Page 22: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Convert HTML to node<script type="text/javascript">

//create a new <div> nodevar div = document.createElement("div");//set background color of <div> to reddiv.style.backgroundColor = "red";//create a <p> nodevar p = document.createElement("p");p.innerHTML = "I am some text that will be inflated";//find <body>var body = document.getElementByTagName("body")[0];//attach <p> to <div>div.appendChild(p);//attach <div> to <body>body.appendChild(div);

</script>

<div>bg-color: red

<p>#text: I am…

<body>

Page 23: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Inflation: Going from HTML to Node Object

<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

snippet.html

Inflater Machine

Page 24: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Objects

Inflation: Going from HTML to Node Object

<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

snippet.html

HTML

Inflater Machine

<div>bg-color: red

<p>#text: I am…

Page 25: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

Bringing it all together

html

body

p <div>bg-color: red

<p>#text: I am…

Page 26: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

<html> <body> <p>Let's explain how Inflation works!</p>

<div style="background-color:red"> <p>I am some text that will be inflated</p></div>

</body></html>

html

body

p <div>bg-color: red

<p>#text: I am…

Page 27: How Inflation Works!. Examine the following code public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState)

((Activity)context).getLayoutInflater().inflate(R.layout.rating_text_view, this);

In Android World