Practice English Speaking&Listening with: Do XML ao Java

Difficulty: 0

Now we know that we can refer to XML resources in the Java code.

But what exactly is the relationship between the resources and the Java code?

How do they come together as part of a full app experience?

Let's start at the point when you click on the JustJava app icon.

It opens up the Main Activity.

Now you don't see anything yet because it's still being initialized.

Thankfully, a lot of the complexity involved in initializing the activity is

handled automatically for you by Android.

The important part that we care about is that in the Main Activity file,

the onCreate method gets called automatically.

This is when the activity is getting created.

There's a bunch of work and then we have an important line that says

setContentView and then the resource ID for the layout file.

It's R.layout.activity_main.

This means that the content view of the activity should be set to the layout

as specified in the activity main XML file.

This is the method call and the input parameter is this resource ID.

Once you set the lay out that you're going to use for the content view,

then the android device can go ahead and start parsing this XML layout file.

Parsing is like reading this file and

trying to interpret that like oh this is a linear layout.

Oh this is a text view.

These are the attributes, and so on.

So it's trying to understand what this file contains.

The Android device starts by reading at the top of the file.

It recognizes that there's a linear layout here.

Then it inflates a Java object to represent that linear layout.

Then it goes to the next line and

sees that we need to add a TextView as a child to that linear layout, like that.

This is starting to look like the view hierarchy from lesson two.

What I didn't tell you earlier is that this is actually a hierarchy

of Java objects.

We're going to be learning more about Java objects in the coming videos so

it's okay if you don't fully understand it yet.

When we get to the next line we see that we need to inflate

a linear layout Java object.

That gets added as a child to this root LinearLayout.

Then we see a Button element.

So we add that as a child to the LinearLayout.

So we continue down the file until we've inflated all the views that we need for

our view hierarchy.

Once we have this view hierarchy of Java objects, then we can manipulate them and

change them while the app is running.

An object like the TextView here contains state information.

Like the text, the text color, the text size.

The object also has methods so we can call those methods and

change their internal state.

So for example, we can change the text while the app is running.

Interacting with all these objects is called object oriented programming.

We will cover this in more detail in the coming videos.

So to summarize, in the main activity when we call setContentView to this

layout, then we read this layout and then inflate a bunch of Java objects.

These Java objects make up the view hierarchy of the app.

The MainActivity holds on to this view hierarchy, and then

as you interact with the MainActivity, it can update these objects.

The Description of Do XML ao Java