In every Android application the resources play the vital role in determining the overall performance and success of application.In this tutorial we will take a look at how the developer can access the android static resources in source code and in XML files.
Using and accessing Android static resources
References to Android resources in Sourcecode
In Android ,the Resources
class allows to access individual resources. An instance of the Resources
class can be retrieved via the getResources()
method of the Context
class. As activities and services extend the Context
class, you can directly use this method in implementations of these components.
An instance of the Resources
class is also required by other Android framework classes. For example, the following code shows how to create a Bitmap
file from a reference ID.
// BitmapFactory requires an instance of the Resource class
BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_find);
Reference to resources in Android XML files
Inside XML files, for example, your layout files, you can refer to other resources via the @
sign.
For example, if you want to refer to a color, which is defined in an XML resource, you can refer to it via @color/your_id
. Or if you defined a String
with the "Username"
key in an XML resource, you could access it via @string/Username
To use an Android system resource, include the android
namespace into the references, e.g., android.R.string.cancel
.