Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Resources
2.1.
Drawables
2.2.
Layout
2.3.
Menu
2.4.
Values
3.
Application Resources
3.1.
String Resource
3.2.
Color Resource
3.3.
Dimension Resource
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Android App Resources

Author Vasu Bansal
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will discuss about the resources of our Android application. Other than the essential code components of our application, an Android application also contains a lot of non-coding assets like images, drawables, colours, etc. We will look at all of these resources in detail and study how to retrieve/extract these resources.

To read more about the architecture of Android, refer to the blog Android Architecture on the Coding Ninjas Website.

Also see, android operating system

Types of Resources

There are many resources in our Android application. Some of the most common types of resources are listed below.

Drawables

These resources are present in the app/res/drawable directory. It contains the XML files that provide graphics of our application. A sample snapshot of the file structure under the drawable folder is shown below.

Layout

These resources are present in the app/res/layout directory. It contains the XML files that define the layout of the user interface. A sample snapshot of the file structure under the layout folder is shown below.

Menu

These resources are present in the app/res/menu directory. It contains the XML files that define the menus and action bar items. A sample snapshot of the file structure under the menu folder is shown below.

Values

These resources are present in the app/res/values directory. It contains the XML files that contain values such as strings, colours and integers. A sample snapshot of the file structure under the values folder is shown below.

The four important XML files in the values folder are:

  • colors.xml
  • dimens.xml
  • strings.xml
  • themes.xml
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

  1. 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.
     
  2. 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.
     
  3. 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!!

Previous article
Android App Components and Folder Structure
Next article
Activity-lifecycle Events
Live masterclass