Android Resource

Resource:

You should always externally use app resources such as images, color and strings ctc. from your code, so that you can maintain them separately and independent. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses the preferred resource based on the app configuration.
When we create an Android project. The Resource file will be created by Android Studio. Names it as “res”. The resource file has three sub folder

Below is the sample example of the resource file with hierarchy for a simple project:

 

MyProject/
    src/
        MyActivity.java
    res/
        drawable/
            graphic.png
        layout/
            main.xml
            info.xml
        mipmap/
            icon.png
        values/
            strings.xml

1.drawable
2. layouts
3.values
Each of the folders in this section has its importance. Format and syntax for application resource that you can provide in your resource directory. “res/” .You can also add alternative versions of each file that are optimized for different device configurations (such as a high-res version of a bitmap for high-density screens). Android Studio helps you add new resources and alternative resources in several ways, depending on the type of resource you want to add.

Resource Animations:

Animations are saved in res/anim folder and accessed from R.anim class. frame animations are saved in res/drawable/ folder and accessed from R,drawable class

Resource Color:

If you want to add background colors & front end for UI look and feel etc to your app. res/color folder used to save color and accessed from R.color class

Resource drawables :

drawables are used to save images to use them into the app for more look and feel. Images are saved in res/drawable folder and accessed from R.drawable class

res/
    drawable/
        icon.png
        background.png
    drawable-hdpi/
        icon.png
        background.png

The hdpi  is named as a qualifer and it indicates that the resources in that directory for devices with a high-density screen. The images in each of these drawable directories are sized for a specific screen density, but the filenames are exactly the same. The resource ID that you use to reference the icon.png or background.png image is always the same, but Android selects the version of each resource that best matches the current device, by comparing the device configuration information with the qualifiers in the resource directory name.

Android has many qualifiers and you can add multiple qualifiers to one directory name, by separating each qualifier with a dashi.e. “-” .

  • The  resource directories cannot be nested. For example, you cannot have res/drawable/drawable-en/.
  • The resource Values are case-insensitive. The resource compiler converts directory names to lower case before processing to avoid problems on case-insensitive file systems. The y capitalization in the names  are useful for reading only.  each qualifier type has Only one value supported.

Resource Layout:

Layout resource used to give beautiful design to your UI of for better look and feel.
layouts are saved in res/layout and accessed from R.layouut class

Resource Menu:

Menu’s contains list of importent Information about the application. menu’s are saved in res/menu folder and accessed from R.menu class

Resource Font :

Define font families and include custom fonts in XML. Saved in res/font/ and accessed from the R.font class.

Resource More Types :

Define other primitive values as static resources, including the following:

Resource Bool :

XML resource file that has a boolean value. saves in res/bool folder and accessed from R.bool class

Resource Color :

XML resource file that has a color value (a hexadecimal color). saves in res/color folder and accessed from R.color class

Resource Dimension :

XML resource file that has a dimension value (with a unit of measure). it will be accessed from R.dimen class

Resource Id:

XML resource that provides a unique identifier for application resources and components to indentify it. and it will be accessed from R.java class

Resource Integer:

XML resource file that contains an integer value and it will be accessed from R.java class

Resource Array:

XML resource that provides an array of integers and will be accessed from R.java class.

Resource Typed Array :

XML resource that provides a TypedArray and will be accessed from R.java class and used for Array of drawables

Resources String :

A string resource provides text strings for your application with optional text styling and formatting. String resource are saved in res/values folder and accessed from R.stringclass.
There are three types of resources that can provide your application with strings:

String :

XML resource that provides a single string.

String Array :

XML resource that provides an array of strings.

Quantity Strings :

XML resource that carries different strings for pluralization.
All strings are capable of applying some styling markup and formatting arguments

Style resource :

A style resource defines the format and look for a UI. A style can be applied to an individual
View (from within a layout file) or to an entire. Activity or application (from within the manifest file).

Resources Animation :

An animation resource can define one of two types of animations:

Resource Property Animation :

Creates an animation by modifying an object’s property values over a set period of time with an Animator.

Resource View Animation :

There are two types of animations that you can do with the view animation framework:

Tween animation:

Creates an animation by performing a series of transformations on a single image with an Animation

Frame animation:

creates an animation by showing a sequence of images in order with an Animation Drawable.

________________________________________________________________________________

Creating alias resources :

When you have a resource that you’d like to use for more than one device configuration,  you can (if required in some cases) create an alternative resource file  that acts as an alias for a resource saved in your default resource directory.

All the resources does not offer this features to create alias for other resources. animation, menu, raw, and other unspecified resources in the xml/ directory don’t offer this feature.

Creating alias for drawable:

below is the sample code to create an alias to an existing drawable, use the <drawable> element.:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="icon">@drawable/icon_ca</drawable>
</resources>

save above file as drawable.xml file. when  it is compiled into a resource file that you can reference as R.drawable.icon, but is actually an alias for the R.drawable.icon_ca resource.

Creating alias forLayout:

To create an alias to an existing layout, use the <include> element, wrapped in a <merge>. For example:

<?xml version="1.0" encoding="utf-8"?>
<merge>
<include layout="@layout/main_ltr"/>
</merge>

If you save this file as main.xml, When it is compiled into a resource you can reference as R.layout.main, but is actually an alias for the R.layout.main_ltr resource file,

Creating alias for String:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello</string>
<string name="hi">@string/hello</string>
</resources>

The R.string.hi resource is now an alias for the R.string.hello.

Creating Alias for color:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#f00</color>
<color name="highlight">@color/red</color>
</resources>

Using resources in java code ot activity class:

You can use a resource in code by passing the resource ID as a method parameter.

ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

Adding String Resource to the Button Below example shows how to add in XML file :

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/submit" />

  For example, if you have the following resource file that includes a color resource and a string resource:

To add them at a time use the sample xml resource file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>

Below is the sample code to use above resource file in below layout file

 

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />

 

Note: You should use string resources at all times, so that your application can be localized for other languages. For information about creating alternative resources (such as localized strings). For a complete guide to localizing your application for other languages.

Accessing platform resources:

Android has a number of standard resources, for example  styles, themes, and layouts etc. To access these resource, qualify your resource reference with the android package name. For example, Android provides a layout resource you can use for list items in a ListAdapter:

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));

In above example, simple_list_item_1 is a layout resource defined by the platform for items in a ListView. You can use this instead of creating your own layout for list items.

________________________________________________________________________________

Leave a Reply

Categories