So chances are when you're building your application you are going to be using
some type of external resources.
Now these might be video files, audio files, images, XML files, but it's likely
you are going to be using some of these types of assets,
so it's good to know how to actually reference assets in your application.
So as I've shown you already in a couple of movies, the resources for your
application are stored in this resource directory, which is just res for short,
and some of the ones that are included automatically are layout.
This is where our XML layouts are which define our UI.
Inside of the values folder, this is where we actually have values that are used
throughout our application, and here's one called strings.xml.
So the way this works is we have an XML file here, and we have certain string
resources. So any where in my application where I want it to say Hello World Main,
I am actually going to reference this resource.
So why do we keep it external?
Well, obviously for globalization or internationalization of our application, we
can actually provide different folders for different languages.
That way, all we have to do is to go and to create a new XML file in a
different language.
We don't have to go through all of our application code and fix things
that we've hard coded.
But there are lots of other types of resources that we can store in here.
So I am quickly go to the Android Developer site.
One of the things you are going to want to get very familiar with is this Dev Guide.
This contains tons and tons of useful information for you as you are
building applications.
So I am going to go to Application Resources here and then click on
Providing Resources.
This is just a good overview of how resources are used and accessed in Android.
But of particular interest, if we scroll down, here we have the different types
of directories that are supported in the Resources directory.
So we have an anim folder, which is obviously short for animation, and this is
where we can provide XML files that define animations, and we are going to be
getting into that in a later movie.
If we want to have a list of colors that our application uses in XML, we can put
it in a color folder.
Any of our images, or even XML-defined graphics, are in the drawable directory.
Layouts contain our XML layouts.
If we want to create menus, like option menus, which we will be doing in another
movie, we are going to save them into the menu directory.
raw is the folder where we actually store raw streams of data, and this is useful
for video and audio, so things that are accessed just as raw streams of data.
And then values, like I mentioned contains, simple values like strings,
integers, and colors.
And then lastly we have an xml directory if we want to bundle XML files with our
application and reference them, we use the XML directory.
Now you may be wondering right here what is this assets folder?
Well, assets folder is basically a folder that you can put resources in, but
they're not referenced by an ID in the application.
There are no IDs generated for those resources.
And in general, I like it best to keep all of my resources inside of this res directory.
So let's actually bring in a few different types of assets.
So if we look in the exercise files, and we are on the desktop here, go into
exercise files, into the assets folder, there are some assets that I have
included for you, and they range from audio files, to video files, to images.
Let's just import some of these.
So the easiest way to import things into our Android project here is just to select them.
Let's say I want to import these two images. And I am just going to drag them
into the drawable directory, and it's going to ask us, "How do we want to import
them into the project?"
We want to copy those files, and now we have them as part of our project.
Now one of things I am sure you are noticing is this directory is not drawable.
It's drawable-hdpi. And this is for high-DPI devices, and then we have another
folder for low-DPI devices, and medium-DPI devices, and this is another
powerful feature of Android.
Basically, we can put different assets that we want to use for these different
DPI screens, and the system will automatically pull out the correct ones based on
the user's actual hardware that they're using.
And we are going to get into this in another movie, but just know that by
putting them in the hdpi folder, even if you are on a medium- or low-DPI device,
it is still going to use these.
That's all you need to worry about for now.
So now, if we actually go in this gen folder, you can see--and this is the
file, like I mentioned before, that you don't want to edit, but it is helpful to look at--
we can see that what happens as you add resources to your project, Android is
actually adding properties in here to reference.
So we have that Amsterdam photo, and it's created an ID that we can use to
reference it throughout our project.
Now we don't actually give this number in our project. We actually use another
method to actually get to this ID, so let's take a look at that.
I have just created a blank project here.
There has been nothing done to this project, except we just imported those assets.
So I am going to go into the layout, and I am going to get rid of this TextView.
Another thing to know about this graphical layout here is that we can change
the size of our display to see how it's going to look on different sizes, so now it's QVGA.
If we wanted to see what it looked like on a WVGA, which what our emulator is
at, we can choose that.
Now this is a little big for my resolution here, so I am just going to lower here,
to something like that. This is fine.
So what we are going to do is we are going to add an ImageView to our layout here.
Again, this is something that allows us to, obviously to display an image.
Well, let's say I want to display this Amsterdam image that I imported.
So I can do this by going into the properties here, and this is the properties
for this ImageView. And you'll notice if I come down, we have a source property.
By default, it's actually defaulting to the icon image that ships by default
with every application.
What I am going to do is change it.
So I am just going to click here and click on this button, and this is going to
allow me to choose any items from my drawable directory.
So I am going to choose Amsterdam,
click OK, and now we have our image inside of here.
So again, that's how we actually reference things in the XML.
Now let me actually show you in the XML view what that looks like.
So here we can see we have the source property.
When we are actually referencing things, we are going to use an @ symbol, and now
we can choose from the different directories, but I want to go into drawable.
Again, I can hit Ctrl+Space to get code hinting.
So we are saying @ the drawable directory, give me the Amsterdam image.
Now remember, we don't say drawable-hdpi here.
Again, the system takes care of that. So our application only references the
drawable folder, and then the Android system will choose the correct size
graphic to match whatever the user's device is.
So that's how to reference it from the XML.
Let me show you how to reference it from the Java code.
So let's go to our Java code, and let's say as soon as we come into our activity,
in the onCreate, let's go ahead and set the imageView.
So I am going to get a reference to that, iv, and again, to get our layout, we are
going to use findViewById R.id. There's our imageView.
Again, we are going to have to cast that to imageView.
Now let's say we actually want to set the image to the Lima photo, which is the
other photo in there. I can do setImageResource.
So now we are going to set that image equal to one of the resources that I
have in my project.
Now to reference it from here, we're going to reference it to that R class, which
is autogenerated. Then we are going to go to drawable and then to lima.
So this, obviously we are only going to able to test it when we run the application, so
let's go ahead and do that, and let's bring up the emulator.
Because in our XML we referenced the Amsterdam photo, again after the onCreate
is called, we are actually changing it and say no, show me that lima.jpg image,
which we can see here. And this is just an energy drink that you can get in Lima, Peru.
So again, this is how we reference resources, both from the XML and from the Java code.
Now these things are slightly different based on the type of resource and how
you are going to handle that resource, but just remember, whenever you add stuff
to the resource directory, behind the scenes, Android is creating this file, which
holds all of the references to your resources and the IDs, so that you are only
ever referencing R.drawable.lima here.
So the resource system takes a little bit of getting used to, but it becomes
very, very powerful, especially when you're targeting different devices and