1 00:00:01,000 --> 00:00:04,000 So chances are when you're building your application you are going to be using 2 00:00:04,000 --> 00:00:07,000 some type of external resources. 3 00:00:07,000 --> 00:00:12,000 Now these might be video files, audio files, images, XML files, but it's likely 4 00:00:12,000 --> 00:00:15,000 you are going to be using some of these types of assets, 5 00:00:15,000 --> 00:00:19,000 so it's good to know how to actually reference assets in your application. 6 00:00:19,000 --> 00:00:23,000 So as I've shown you already in a couple of movies, the resources for your 7 00:00:23,000 --> 00:00:29,000 application are stored in this resource directory, which is just res for short, 8 00:00:29,000 --> 00:00:33,000 and some of the ones that are included automatically are layout. 9 00:00:33,000 --> 00:00:36,000 This is where our XML layouts are which define our UI. 10 00:00:36,000 --> 00:00:42,000 Inside of the values folder, this is where we actually have values that are used 11 00:00:42,000 --> 00:00:46,000 throughout our application, and here's one called strings.xml. 12 00:00:46,000 --> 00:00:52,000 So the way this works is we have an XML file here, and we have certain string 13 00:00:52,000 --> 00:00:57,000 resources. So any where in my application where I want it to say Hello World Main, 14 00:00:57,000 --> 00:01:00,000 I am actually going to reference this resource. 15 00:01:00,000 --> 00:01:01,000 So why do we keep it external? 16 00:01:01,000 --> 00:01:07,000 Well, obviously for globalization or internationalization of our application, we 17 00:01:07,000 --> 00:01:11,000 can actually provide different folders for different languages. 18 00:01:11,000 --> 00:01:16,000 That way, all we have to do is to go and to create a new XML file in a 19 00:01:16,000 --> 00:01:17,000 different language. 20 00:01:17,000 --> 00:01:20,000 We don't have to go through all of our application code and fix things 21 00:01:20,000 --> 00:01:21,000 that we've hard coded. 22 00:01:21,000 --> 00:01:27,000 But there are lots of other types of resources that we can store in here. 23 00:01:27,000 --> 00:01:30,000 So I am quickly go to the Android Developer site. 24 00:01:30,000 --> 00:01:34,000 One of the things you are going to want to get very familiar with is this Dev Guide. 25 00:01:34,000 --> 00:01:38,000 This contains tons and tons of useful information for you as you are 26 00:01:38,000 --> 00:01:40,000 building applications. 27 00:01:40,000 --> 00:01:43,000 So I am going to go to Application Resources here and then click on 28 00:01:43,000 --> 00:01:45,000 Providing Resources. 29 00:01:45,000 --> 00:01:51,000 This is just a good overview of how resources are used and accessed in Android. 30 00:01:51,000 --> 00:01:56,000 But of particular interest, if we scroll down, here we have the different types 31 00:01:56,000 --> 00:02:00,000 of directories that are supported in the Resources directory. 32 00:02:00,000 --> 00:02:04,000 So we have an anim folder, which is obviously short for animation, and this is 33 00:02:04,000 --> 00:02:08,000 where we can provide XML files that define animations, and we are going to be 34 00:02:08,000 --> 00:02:11,000 getting into that in a later movie. 35 00:02:11,000 --> 00:02:15,000 If we want to have a list of colors that our application uses in XML, we can put 36 00:02:15,000 --> 00:02:17,000 it in a color folder. 37 00:02:17,000 --> 00:02:23,000 Any of our images, or even XML-defined graphics, are in the drawable directory. 38 00:02:23,000 --> 00:02:26,000 Layouts contain our XML layouts. 39 00:02:26,000 --> 00:02:30,000 If we want to create menus, like option menus, which we will be doing in another 40 00:02:30,000 --> 00:02:33,000 movie, we are going to save them into the menu directory. 41 00:02:33,000 --> 00:02:39,000 raw is the folder where we actually store raw streams of data, and this is useful 42 00:02:39,000 --> 00:02:44,000 for video and audio, so things that are accessed just as raw streams of data. 43 00:02:44,000 --> 00:02:48,000 And then values, like I mentioned contains, simple values like strings, 44 00:02:48,000 --> 00:02:50,000 integers, and colors. 45 00:02:50,000 --> 00:02:55,000 And then lastly we have an xml directory if we want to bundle XML files with our 46 00:02:55,000 --> 00:02:59,000 application and reference them, we use the XML directory. 47 00:02:59,000 --> 00:03:03,000 Now you may be wondering right here what is this assets folder? 48 00:03:03,000 --> 00:03:08,000 Well, assets folder is basically a folder that you can put resources in, but 49 00:03:08,000 --> 00:03:11,000 they're not referenced by an ID in the application. 50 00:03:11,000 --> 00:03:15,000 There are no IDs generated for those resources. 51 00:03:15,000 --> 00:03:21,000 And in general, I like it best to keep all of my resources inside of this res directory. 52 00:03:21,000 --> 00:03:25,000 So let's actually bring in a few different types of assets. 53 00:03:25,000 --> 00:03:31,000 So if we look in the exercise files, and we are on the desktop here, go into 54 00:03:31,000 --> 00:03:35,000 exercise files, into the assets folder, there are some assets that I have 55 00:03:35,000 --> 00:03:40,000 included for you, and they range from audio files, to video files, to images. 56 00:03:40,000 --> 00:03:41,000 Let's just import some of these. 57 00:03:41,000 --> 00:03:48,000 So the easiest way to import things into our Android project here is just to select them. 58 00:03:48,000 --> 00:03:54,000 Let's say I want to import these two images. And I am just going to drag them 59 00:03:54,000 --> 00:03:59,000 into the drawable directory, and it's going to ask us, "How do we want to import 60 00:03:59,000 --> 00:04:00,000 them into the project?" 61 00:04:00,000 --> 00:04:05,000 We want to copy those files, and now we have them as part of our project. 62 00:04:05,000 --> 00:04:09,000 Now one of things I am sure you are noticing is this directory is not drawable. 63 00:04:09,000 --> 00:04:16,000 It's drawable-hdpi. And this is for high-DPI devices, and then we have another 64 00:04:16,000 --> 00:04:21,000 folder for low-DPI devices, and medium-DPI devices, and this is another 65 00:04:21,000 --> 00:04:23,000 powerful feature of Android. 66 00:04:23,000 --> 00:04:27,000 Basically, we can put different assets that we want to use for these different 67 00:04:27,000 --> 00:04:33,000 DPI screens, and the system will automatically pull out the correct ones based on 68 00:04:33,000 --> 00:04:35,000 the user's actual hardware that they're using. 69 00:04:35,000 --> 00:04:39,000 And we are going to get into this in another movie, but just know that by 70 00:04:39,000 --> 00:04:44,000 putting them in the hdpi folder, even if you are on a medium- or low-DPI device, 71 00:04:44,000 --> 00:04:45,000 it is still going to use these. 72 00:04:45,000 --> 00:04:47,000 That's all you need to worry about for now. 73 00:04:47,000 --> 00:04:53,000 So now, if we actually go in this gen folder, you can see--and this is the 74 00:04:53,000 --> 00:04:58,000 file, like I mentioned before, that you don't want to edit, but it is helpful to look at-- 75 00:04:58,000 --> 00:05:02,000 we can see that what happens as you add resources to your project, Android is 76 00:05:02,000 --> 00:05:05,000 actually adding properties in here to reference. 77 00:05:05,000 --> 00:05:10,000 So we have that Amsterdam photo, and it's created an ID that we can use to 78 00:05:10,000 --> 00:05:12,000 reference it throughout our project. 79 00:05:12,000 --> 00:05:17,000 Now we don't actually give this number in our project. We actually use another 80 00:05:17,000 --> 00:05:22,000 method to actually get to this ID, so let's take a look at that. 81 00:05:22,000 --> 00:05:24,000 I have just created a blank project here. 82 00:05:24,000 --> 00:05:28,000 There has been nothing done to this project, except we just imported those assets. 83 00:05:28,000 --> 00:05:34,000 So I am going to go into the layout, and I am going to get rid of this TextView. 84 00:05:34,000 --> 00:05:39,000 Another thing to know about this graphical layout here is that we can change 85 00:05:39,000 --> 00:05:45,000 the size of our display to see how it's going to look on different sizes, so now it's QVGA. 86 00:05:45,000 --> 00:05:50,000 If we wanted to see what it looked like on a WVGA, which what our emulator is 87 00:05:50,000 --> 00:05:52,000 at, we can choose that. 88 00:05:52,000 --> 00:05:57,000 Now this is a little big for my resolution here, so I am just going to lower here, 89 00:05:57,000 --> 00:06:01,000 to something like that. This is fine. 90 00:06:01,000 --> 00:06:06,000 So what we are going to do is we are going to add an ImageView to our layout here. 91 00:06:06,000 --> 00:06:10,000 Again, this is something that allows us to, obviously to display an image. 92 00:06:10,000 --> 00:06:17,000 Well, let's say I want to display this Amsterdam image that I imported. 93 00:06:17,000 --> 00:06:22,000 So I can do this by going into the properties here, and this is the properties 94 00:06:22,000 --> 00:06:29,000 for this ImageView. And you'll notice if I come down, we have a source property. 95 00:06:29,000 --> 00:06:35,000 By default, it's actually defaulting to the icon image that ships by default 96 00:06:35,000 --> 00:06:37,000 with every application. 97 00:06:37,000 --> 00:06:38,000 What I am going to do is change it. 98 00:06:38,000 --> 00:06:42,000 So I am just going to click here and click on this button, and this is going to 99 00:06:42,000 --> 00:06:45,000 allow me to choose any items from my drawable directory. 100 00:06:45,000 --> 00:06:48,000 So I am going to choose Amsterdam, 101 00:06:48,000 --> 00:06:53,000 click OK, and now we have our image inside of here. 102 00:06:53,000 --> 00:06:56,000 So again, that's how we actually reference things in the XML. 103 00:06:56,000 --> 00:07:00,000 Now let me actually show you in the XML view what that looks like. 104 00:07:00,000 --> 00:07:04,000 So here we can see we have the source property. 105 00:07:04,000 --> 00:07:09,000 When we are actually referencing things, we are going to use an @ symbol, and now 106 00:07:09,000 --> 00:07:14,000 we can choose from the different directories, but I want to go into drawable. 107 00:07:14,000 --> 00:07:16,000 Again, I can hit Ctrl+Space to get code hinting. 108 00:07:16,000 --> 00:07:22,000 So we are saying @ the drawable directory, give me the Amsterdam image. 109 00:07:22,000 --> 00:07:27,000 Now remember, we don't say drawable-hdpi here. 110 00:07:27,000 --> 00:07:30,000 Again, the system takes care of that. So our application only references the 111 00:07:30,000 --> 00:07:35,000 drawable folder, and then the Android system will choose the correct size 112 00:07:35,000 --> 00:07:38,000 graphic to match whatever the user's device is. 113 00:07:38,000 --> 00:07:41,000 So that's how to reference it from the XML. 114 00:07:41,000 --> 00:07:44,000 Let me show you how to reference it from the Java code. 115 00:07:44,000 --> 00:07:50,000 So let's go to our Java code, and let's say as soon as we come into our activity, 116 00:07:50,000 --> 00:07:55,000 in the onCreate, let's go ahead and set the imageView. 117 00:07:55,000 --> 00:08:06,000 So I am going to get a reference to that, iv, and again, to get our layout, we are 118 00:08:06,000 --> 00:08:10,000 going to use findViewById R.id. There's our imageView. 119 00:08:10,000 --> 00:08:13,000 Again, we are going to have to cast that to imageView. 120 00:08:13,000 --> 00:08:18,000 Now let's say we actually want to set the image to the Lima photo, which is the 121 00:08:18,000 --> 00:08:26,000 other photo in there. I can do setImageResource. 122 00:08:26,000 --> 00:08:30,000 So now we are going to set that image equal to one of the resources that I 123 00:08:30,000 --> 00:08:31,000 have in my project. 124 00:08:31,000 --> 00:08:35,000 Now to reference it from here, we're going to reference it to that R class, which 125 00:08:35,000 --> 00:08:45,000 is autogenerated. Then we are going to go to drawable and then to lima. 126 00:08:45,000 --> 00:08:49,000 So this, obviously we are only going to able to test it when we run the application, so 127 00:08:49,000 --> 00:08:53,000 let's go ahead and do that, and let's bring up the emulator. 128 00:08:53,000 --> 00:08:58,000 Because in our XML we referenced the Amsterdam photo, again after the onCreate 129 00:08:58,000 --> 00:09:03,000 is called, we are actually changing it and say no, show me that lima.jpg image, 130 00:09:03,000 --> 00:09:08,000 which we can see here. And this is just an energy drink that you can get in Lima, Peru. 131 00:09:08,000 --> 00:09:14,000 So again, this is how we reference resources, both from the XML and from the Java code. 132 00:09:14,000 --> 00:09:19,000 Now these things are slightly different based on the type of resource and how 133 00:09:19,000 --> 00:09:22,000 you are going to handle that resource, but just remember, whenever you add stuff 134 00:09:22,000 --> 00:09:29,000 to the resource directory, behind the scenes, Android is creating this file, which 135 00:09:29,000 --> 00:09:35,000 holds all of the references to your resources and the IDs, so that you are only 136 00:09:35,000 --> 00:09:39,000 ever referencing R.drawable.lima here. 137 00:09:39,000 --> 00:09:43,000 So the resource system takes a little bit of getting used to, but it becomes 138 00:09:43,000 --> 00:09:46,000 very, very powerful, especially when you're targeting different devices and