Managing Android Resources and Assets
By the end of this lesson, you will have a proper understanding of how to manage Android Resources and Assets of an application and how to use them by referring them to the code. In conclusion, we will see another file management mechanism within the project: Assets.
In Android App Development, the Java code often draws internal elements of the project such as XML files, strings, numbers, images and more. The best way to preserve all these “values” available to the application is to place them in the project folder called res and manage them using the appropriate resource mechanism.
Lesson 9 – Managing Android Resources and Assets
Where are the resources?
If you take a look at any Android project you can see that res has several subfolders whose names are not at all random. Among those most commonly used, we find:
- layout: it will contain the graphics architecture of the user interface components. They are defined in XML in a similar way to how HTML is used to structure web pages;
- values: it will contain strings, colors, dimensions and other types of values that can be used in additional resources or in the Java code. Important to note that these values will form the contents of the appropriate XML tags (
<dimen>, etc.) grouped into files from usually indicative name: strings.xml, dimens.xml, colors.xml and so forth. These names are the result of pure convention but the programmer can freely choose how to call them;
- drawable: these are images in the most common formats or, which may be surprising, configured in XML.
How to call Android Resource Files
Resources are compiled in a binary format and indexed using a unique ID. These IDs are stored in a Java class, named
R, self-generated with each change and visible in the gen folder of the Android project. We have already met the R class in the Activity examined below.
Indicated that the layout of the activity was located between the resources. In particular, each path within the R class reflects a placement of resources in the res subfolders as illustrated in the figure.
Through their identifiers, resources are accessible both from Java code and from other resources defined in XML.
For example, the resource of type string and name appname:
<string android:name="appname">Hello world!</string>
can be retrieved, in Java, by
R.string.appnameor from within other XML resources with
Cross-platform adaptation of Android Apps
The fragmentation of the hardware scenario in the Android world remains one of the most difficult obstacles to overcome for the programmer. In this, resources play a very important role. Looking at a typical Android project, you can see that among the folders inside res (figure below) some appear with “canonical” names ( drawable, values, layout ) and others with “modified” names ( mipmap-hdpi, mipmap-mdpi, etc.).
This is because, in the name of the folder, you can append a suffix that represents the configuration of the device with which the limited resources can be recalled.
If, for example, res/layout contains the graphic structure of the various interfaces for any configuration, res/layout-land will contain layouts that can be used only when the device is in landscape position.
Other modifiers of some relevance are those that refer to the language of the device: values-it will be the resources for devices in Italian, values-en for those in English. There is a very large collection of modifiers, all available on the official documentation.
The images deserve a separate discussion. Modifiers applied to drawable folders ( ldpi, mdpi, hdpi and so on) are some of the abbreviations that identify display densities. This concept of density represents the amount of pixels per physical area of the screen. In Android App Development, it is good to abandon the habit of measuring in pixels, using the DP (Density-Independent Pixel) as a unit of measurement for the graphic elements, a kind of “virtual” pixel independent of the density of the display that allows you to keep the proportions intact among the layout elements with varying densities.
Lesson 9 – Managing Android Resources and Assets- Android App Development
The official documentation lists all the types of resources that can be used. There is also a type of “raw” resource that can be placed in the res/raw folder. You will be able to place everything that you cannot fit into a particular type. As an alternative to raw resources, Android Assets can be defined. These are outside the resource mechanism and must be deposited in the project folder of the same name. They are neither compiled in binary format nor labeled with a unique ID. Their use by the application will take place through a stream that can be requested to a Java class named AssetManager.
How useful was this Lesson?
Click on a star to rate it!
Average rating / 5. Vote count:
Be the first to rate this post.!
We are sorry that this lesson was not useful for you!
Let us improve this lesson!
Tell us how we can improve this lesson?
Robort Gabriel is an expert on Android & Web Development. He is also an Entre-Programmer and Web Strategist. He decides to found Coding180 with the aim of inspiring people to learn to program in order to create a future generation of entrepreneurs who are able to move with agility in the digital world.