What is Resource File in Android?

What Are Resources?

All Android applications are composed of two things: functionality (code instructions) and data (resources).
The functionality is the code that determines how your application behaves. This includes any algorithms that make the application run. Resources include text strings, images and icons, audio files, videos, and other data used by the application.


Storing Application Resources

Android resource files are stored separately from the java class files in the Android project.
Most common resource types are stored in XML.
Store raw data files and graphics as resources.Understanding the Resource Directory Hierarchy Resources are organized in a strict directory hierarchy within the Android project.
All resources must be stored under the /res project directory in specially named subdirectories that must be lowercase. Different resource types are stored in different directories.
The resource sub-directories generated when you create an Android project using the Eclipse plug-in.

Default Android Resource Directories

 Each resource type corresponds to a specific resource subdirectory name.For example, all graphics are stored under the /res/drawable directory structure.For example, the /res/drawable-hdpi directory stores graphics for high-density screens /res/drawable-ldpi directory stores graphics for low-density screens, and the  /res/drawable-mdpi directory stores graphics for medium-density screens.
If you had a graphic resource that was shared by all screens, you would simply store that resource in the /res/drawable directory. Using the Android Asset Packaging Tool the plug-in detects new resources when you add them to the appropriate project resource directory under /res automatically.
These resources are compiled, resulting in the generation of the R.java file, which enables you to access your resources programmatically.
aapt tool in the /tools subdirectory of each specific Android SDK version command- line interface to compile your resources and package your application binaries to deploy to the phone or emulator.

Resource Value Types

 Android applications rely on many different types of resources—such as text strings, graphics, and color schemes—for user interface design. These resources are stored in the /res directory of your Android project in a strict (but reasonably flexible) set of directories and files.
All resources filenames must be lowercase and simple (letters, numbers, and underscores only).
Storing Simple Resource Types Strings Each resource file under the /res/values directory should begin with the following XML header:
<?xml version=”1.0” encoding=”utf-8”?>
root node is <resources> followed by specific resource type elements such as <string> or <color>.
Storing Graphics, Animations, Menus, and Files store numerous other types of resources, such as animation sequences, graphics, arbitrary XML files, and raw files.
These types of resources are not stored in the /res/values directory instead stored in specially named directories according to their type.
E.g. animation sequence definitions in the /res/anim directory.
How Resources Are Resolved An android project resources can be organized based upon more than a dozen different types of criteria,
–including language and region,
–screen characteristics,
–device modes (night mode, docked, and so on),
– input methods, and
–many other device differentiators.
Resources stored at the top of the resource hierarchy as default resources and the specialized versions of those resources as alternative resources common reasons that developers use alternative resources are:
“for internationalization and localization purposes and to design an application that runs smoothly on different device screens and orientations.”

Default resources example:

The resources are stored in the top-level resource directories (for example, /res/values
/strings.xml, /res/drawable/myLogo.png, and /res/layout/main.xml).
No matter what Android device (huge hi-def screen, postage-stamp-sized screen, English or Chinese language or region, portrait or landscape orientation, and so on), you run this application on, the same resource data is loaded and used.

Alternative resources example:

Create alternative string resources in Chinese simply by adding a second strings.xml file in a resource subdirectory called /res/values-zh/strings.xml (note the –zh qualifier).
Provide different logos for different screen densities by providing three versions of myLogo.png:
  •  /res/drawable-ldpi/myLogo.png (low-density screens)
  •  /res/drawable-mdpi/myLogo.png (medium-density screens)
  •  /res/drawable-hdpi/myLogo.png (high-density screens)
The application would look much better if the layout was different in portrait versus landscape modes change the layout around, moving controls around, in order to achieve a more pleasant user experience, and provide two layouts:
/res/layout-port/main.xml (layout loaded in portrait mode)
/res/layout-land/main.xml (layout loaded in landscape mode)

Android behavior for alternative resources

With these alternative resources in place, the Android platform behaves as follows:
If the device language setting is Chinese, the strings in /res/values-zh/ strings.xml are used. In all other cases, the strings in /res/values/strings.xml  are used.
If the device screen is a low-density screen, the graphic stored in the /res/drawable-ldpi/myLogo.png resource directory is used.
If it’s a medium-density screen, the mdpi drawable is used, and so on.
If the device is in landscape mode, the layout in the /res/layout-land/main.xml is loaded.
If it’s in portrait mode, the /res/layout-port/main.xml layout is loaded.

0 comments:

Post a Comment

 
Design by Wordpress Theme | Bloggerized by Free Blogger Templates | Grocery Coupons