Application Resources
Depending on the type of resource, the application resources can be defined as shown below.
String Resource
It would be best to define the text you want to display in your application in the strings.xml file (for example, button labels or text inside TextView). Each item consists of a key-value pair where the key indicates the ID of the text and the value indicates the text itself. The following is a sample code written inside the strings.xml file of an Android application.
Code:
<resources>
<string name="app_name">Hello World</string>
<string name="action_settings">Settings</string>
<!-- Strings used for fragments for navigation -->
<string name="first_fragment_label">First Fragment</string>
<string name="second_fragment_label">Second Fragment</string>
<string name="next_button">Next</string>
<string name="previous_button">Previous</string>
<string name="hello_first_fragment">Hello first fragment</string>
<string name="hello_second_fragment">Hello second fragment. Arg: %1$s</string>
</resources>
If you will reference the string resource with the label first_fragment_label, you would retrieve the value as First Fragment. Similarly, if you will reference the string resource with the label next_button or previous_button, you would retrieve the value as Next and Previous, respectively. The strings can be accessed using the following line of code.
Code:
String next_button_label = getResources().getString(R.string.next_button)
Color Resource
The color resources are defined in the colors.xml file, which is present in the app/res/values directory. It contains all the colours that are used in our application. The following is a sample code written inside the colors.xml file of an Android application.
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
As can be seen from the above code snippet, the application uses colours such as white, black, different shades of purple and teal. The colours can be accessed using the following line of code.
Code:
int color = ContextCompat.getColor(context, R.color.black);
Dimension Resource
The dimension resources are defined in the dimens.xml file, which is present in the app/res/values directory. It contains all the details regarding the dimensions used inside our application. The following is a sample code written inside the dimens.xml file of an Android application.
Code:
<resources>
<dimen name="fab_margin">16dp</dimen>
</resources>
The dimensions can be accessed using the following line of code.
Code:
Resources res = getResources();
float margin = res.getDimension(R.dimen.fab_margin);
FAQs
-
What are property animations in Android?
These are a type of resource present in the animator directory. They are responsible for defining the property animations.
-
What is Dynamic Resource Retrieval in Android?
This concept is used to retrieve a resource's value dynamically rather than hardcoding it. The getIdentifier method helps us in achieving this target.
-
What are the most commonly used qualifiers in an Android application?
The most commonly used qualifiers include screen size (sw480dp, sw600dp), screen rotation (port, land), screen density (hdpi, xhdpi), etc. We can even specify multiple qualifiers for one set of resources.
Key Takeaways
Cheers if you reached here!! This blog introduces you to the different types of resources present in an Android application. You can also read the blog Android App Components and Folder Structure on the Coding Ninjas website to learn more about the components of an Android application and the associated directory structure of the project.
Check out this problem - Multiply Strings
Yet there is never an end to the learning process, so check out our Android Development Course on the Coding Ninjas Website to learn everything you need to know about Android development and how to design the applications of the future. Until then, good luck!!