Accessing Resources
During your application development
you will need to access defined resources either in your code, or in your
layout XML files. Following section explains how to access your resources in
both the scenarios:
ACCESSING RESOURCES IN CODE:
When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
EXAMPLE:
To access res/drawable/myimage.png and set an ImageView you will use following code:
To access res/drawable/myimage.png and set an ImageView you will use following code:
ImageView imageView = (ImageView)
findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage); |
Here first line of the code makes
use of R.id.myimageview to get ImageView defined with idmyimageview in a Layout
file. Second line of code makes use of R.drawable.myimage to get an image with
name myimage available in drawable sub-directory under /res.
EXAMPLE:
Consider next example where res/values/strings.xml has following definition:
Consider next example where res/values/strings.xml has following definition:
< ?xml version="1.0"
encoding="utf-8"? >
< resources > < string name="hello" > Hello, World! < /string > < /resources > |
No comments:
Post a Comment