[MUSIC] Another kind of resource is a layout file. Layout files specify what the user interface for some part of your application will look like. And again, these files are written in XML. Although some tools will allow you to create the layout visually. And then those tools will generate the XML for you. In fact, Eclipse will do that. Layout files are typically stored in the res/layout directory of your application. And you can access the layout in Java as R.layout.layout_name. And you can access that layout in other resource files as &layout/ layout name. Now just like string files whose use depends on your default language, Android allows you to create multiple layout files, and then Android can choose from those files at runtime based on your device's configuration. Let's look at an example of that. So let's look at the main.xml file that's in the res/layout directory. This file specifies that the layout is composed of something called the RelativeLayout, one that when we talk about user interfaces. And inside the relative layout, there's another element called an edit text. That's the box that you use for entering the postal address. There's also a button, that's the button that was labeled show map. Eclipse can also show what it thinks this will all look like at runtime. And you can see that by clicking on the graphical layout tab. Now once you're there, you can also click on the individual elements, and Eclipse will show you more detailed information about their layout properties. Map location also has another file, also called main.xml but this one is in the res/layout-land directory. If map location is running with a device in landscape mode, this file is used instead of the one that we saw before. Now as you can see, this file uses the same elements, a relative layout, an edit text, and a button, but I've changed their positioning slightly so that the elements all appear on a single line, which I think looks better in landscape mode. And again, you can play around with Eclipse to get more details about the layout properties of each individual element. Now let's run the application. The phone on the left is running in portrait mode, so it uses the default main.xml file. The phone on the right, however, is running in landscape mode. So it will use the main.xml file that's in res/layout-land. And as you can see the elements are actually laid out differently depending on the orientation of the device. Now I've mentioned a few times now that resources can be accessed in Java as R.something or another. Well, to do this Android generates a class called R, from your application. And you can then access the fields of this R class to get to those strings and layouts and other resources that you defined in the XML files. Let's take a look at an, at an actual R.Java file. As I've said, this file is generated by Android so you shouldn't modify it. You can see that the file defines the R class and this R class contains another class called Layout. Which has a field called Main. And that actually gives you a reference or handle to the main.xml file. There's also an id class providing handles to the relative layout, to the edit text box, and to the button defined in the main.xml files. In fact, if you go back and check the main.xml files now, you'll see that there are lines that say Android:id. And that's where these fields and IDs come from. And finally, there's a class called string, which provides handles for all the strings that we've been talking about. The next step in application development is implementing your Java classes. And this usually involves writing at least one activity. The entry point for activities is the onCreate method. So that's where you'll typically initialize your application. Let's look at the onCreate method in a bit more detail. In onCreate, you usually do the following four things. You restore saved application state. You set the content view, which tells Android what to display as the activities user interface. You initialize specific elements of your activities user interface, and last, you attach code to those user interface elements so that specific actions will be performed when the users interact with these user interface elements. Let's see how these steps are implemented in MapLocation. So, let's take a look at the MapLocation source code. It's all contained in a file called MapLocation.java in the application's source directory. Now, as you can see this file implements a class called MapLocation, which is a subclass of activity. As an activity it has an onCreate method, where the application is initialized. The first step of onCreate is to call super.onCreate passing saved instance state as a parameter. Now this saved instance state is a, something of type bundle and it's basically a data structure containing any information that Android might have saved from the last time the activity was running. We'll talk more about this next time when we dive deeper into the activity class. But for now just be aware that onCreate has to call super.onCreate or you'll get an error. Next there's a call to setContentView. In this case passing in the reference of the layout file for this application, R.layout.main. After that there's some code that acquires references to individual UI elements in the layout. Such as the edit text box, which is stored in a variable called addrText. And the button, which is stored in a variable called button. As you can see, these references are acquired by calling activity.findViewById. And passing in the id of the desired element. And finally there's some code that defines what to do when the user presses the show map button. This code implements the on click listener interface which has an onClick method that gets called whenever the user clicks on this button. And the code in that onClick method, first gets any text that the user has entered in the address box. Then it process that text to remove spaces and put it in a format that Google Maps understands. And, then it starts the Google Map app, Google Maps application passing in this modified address text. And again, I'll explain what this code is doing in more detail in later lessons when we dive into the workings of the activity class and the intent class. And also, just to simplify things, I've left out any error checking or verification of the address string that was input but of course in production code you really going to want to do that. The next step in creating Android applications is to provide information that allows Android's build tools to create the application package, or apk. And this information is written in an XML file called AndroidManifest.XML. The AndroidManifest.XML file contains a wide variety of packaging information, including the name of the application, and a list of the components that make up that application. It also includes other information that we'll discuss later in the course. Things like the permissions that are needed to run this application. The hardware features such as a camera that this application requires and the earliest platform version on which this application runs. Let's look at the AndroidManifest.XML file for map location. The AndroidManifest.XML file is in the top level directory of the application. [SOUND] Let's open it now. [SOUND] Inside, you see that there's a manifest tag, telling Android that this file contains the packaging information that it needs. There's a lot of other information in here as well, but I'll just discuss a few pieces of it. One element we see here is the uses SDK element. This element includes an attribute, min SDK version that specifies the minimum API level for this application. And in this case, that level is ten which corresponds to one of the Android 2.3 releases. This element also includes another attribute, targetSDKVersion, which specifies the latest API level against which this application has been tested. In this case that's level 17 and corresponds to a Android 4.2 release. There's also an application element that specifies things like the icon for this application and the label that's shown in the application's title bar. Inside the application element, there's an activity element that lists the one activity, in this case, map location, that comprises this application. And the last step is installing the application on a device or emulator in order to test and debug it. In an earlier lesson, we saw how to do that in Eclipse. And you can also do this from the command line, for example by issuing commands to the adb tool.