Course Content

Course Content

Description

How To Access Resources In Android

In this Video tutorial we will learn what are the Resources and Assets in Android, and how we can use them both more effectively in our Android application.

So far, we have used resources many times within our application but now we will see what exactly that is. Our basic focus is on coding, but whatever content we are coding, we supply the content with the help of resources.

Bitmaps, colours, layout definitions, user interface strings, animation instructions, and many more are used with the help of resources.

Android Resources Organizing and Accessing

We can always externalize resources such as text strings, layouts, sounds, graphics or any other static data an Android Application may require from our application code so that it becomes easier to maintain and update them independently.

This helps us to alter code without modifying the resources.

Android automatically selects correct resources.

 

Directory

Resource Type

color/

XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class.

drawable/

Images files like .png, .jpg, .gif or XML files are complied into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class.

layout/

XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class.

menu/

XML files that define application menus, such as an options menu, context menu, or sub menu. They are saved in res/menu/ and accessed from the R.menu class.

raw/

Arbitary files to save in their raw form. You need to callResources.openRawResource() with the resource ID, which is R.raw .filename to open such raw files.

values/

XML files that contains simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory.

-arrays.xml

-colors.xml

-dimens.xml

-strings.xml

-style.xml

 

Types of Resources

In Android there are two types of resources,

  1. Default resources
  2. Alternative resources

Default Resources

The resources that should be used to regardless the device configuration or when there are no alternative resources that match the available configuration.

Alternative resources

Resources which support specific device configuration. We will see alternative resources in depth further.

Android resources Organising and Accessing

We need to organise resources in our projects res/directory.

Using various sub directories that group resources by type and configuration.

Alternative Resources

Multiple sets of resources could be included in an application each customised for a different device configuration.

When the user runs the application Android automatically selects and loads the resources which best matches the device.

Your application should provide alternative resources to support specific device configuration.

Many times we can see that our Android application runs on different types of devices, like tablet smartphones etc.. So for that it requires different type of image size according to the screen resolution of particular device. So for that in Alternative resources we can store different images by the help of coding, and it will pick up the required image as per device supports.

Accessing Resources

As an Android application is compiled a 'R' class gets generated.

The R class defines all resource ID for our resources available in the res/directory.

We can provide a resource in application by referencing its resource ID.

Accessing resources in Code

For example, you can set an ImageView to use the res/drawable/myimage.png resource using setImageResource():

  • ImageView imageView=(ImageView) findViewById(R.id.myimage view);

imageView.setImageResource(R.drawable.Myimage);

 

Consider the next example where res/values/strings.xml has following definition.

<?xml version=”1.0” encoding=”utf-8”?>

<resources>

<string name=”welcome”>Welcome to LearnVern!</string>

</resources>

 

Now you can set the text on a TextView object with ID msg using a resource ID as follows.

 

TextView msgTextView=(TextView) findViewById(R.id.msg);

msgTextView.setText(R.string.welcome);

 

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere