dcsimg
December 10, 2016
Hot Topics:

Working with Java Resources in Android Apps

  • December 16, 2015
  • By Vipul Patel
  • Send Email »
  • More Articles »

Introduction to Android Resources

In an earlier article, we learned how to provide resources for an Android application. We also learned how to provide alternate resources. We will now focus on understanding how to access those resources by different ways in our Android application.

Basics of Working with Android Resources

Once a resource is provided to the application, and the application is compiled, Android Asset Packaging Tool (AAPT) provides an identifier, called a resource ID, for each resource, which then can be used to reference that resource.

AAPT generates a class R that contains all these resource IDs. These resource IDs are represented as static integers that are structured based on the type of resource. Drawable resources will be accessible via integer values in the R.drawable subclass.

Resource IDs are made of two elements:

  1. Resource Type: All resources are grouped into one of the many types (which we discussed in an earlier article); for example, drawable, layout and string.
  2. Resource Name: This can be either the filename without the extension, or the value in the XML android:name attribute. (if the resource is a simple value) For example: helloworld.

These resources can be accessed in one of the two ways:

  1. Using static integer values from a sub-class of R: as in R.string.helloworld. We will learn how to specify this in the demo.
  2. Using XML syntax that corresponds to the resource ID. For example, the preceding resource can be represented as @string/helloworld.

To access the resource in code, you use the static integer form of representation. The syntax is <packagename>.R.<resourcetype>.<resourcename>.

This syntax implies that, if the application has permissions, we can access resources from other packages also.

Let us explore more via a simple demo.

Hands On with Resources in Android

Let's create a simple Android application that demonstrates how to access resources.

Fire up Android Studio and start a new Android Studio Project.

Res1
Figure 1: Starting a new Android Studio project

Provide AccessingResourcesDemo as the Application Name and Click Next.

Res2
Figure 2: Naming the application

On the next screen, leave the default values and click Next.

How03
Figure 3: Leaving the default values in place

On the "Add an activity to Mobile" page, choose "Blank Activity". This creates an application with a single activity.

Res4
Figure 4: Creating a new application that has a single activity

We then are prompted to customize the activity. We will leave the default values unchanged.

Res5
Figure 5: Again, leaving the default values in place

Click Finish to creating the project files.

Next, we add a button to the MainActivity. The button will have a resource value assigned to it, and we also will add a click event handler that will change the text of the button to demonstrate how to access the resource in a different way.

Once you add a button to Mainactivity, the XML looks as follows:

<RelativeLayout xmlns:android=
      "http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android:text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="New Button"
      android:id="@+id/button"
      android:layout_below="@+id/textView"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView"
      android:layout_marginTop="70dp" />

</RelativeLayout>

When you hover over the mouse over the highlighted section in Android Studio, you will notice the following text displayed.

Res6
Figure 6: Android Studio is giving you a suggestion

This is Android Studio suggesting that we do not use hardcoded values.

Here is the first change we will make.

Open strings.xml from under /res/values and add a string to it.

//strings.xml

<resources>
   <string name="app_name">AccessingResourcesDemo</string>

   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

Add the following two strings:

<resources>
   <string name="app_name">AccessingResourcesDemo</string>

   <string name="hello_world">Hello world!</string>
   <string name="buttonTextPreClick">I have not been clicked yet!</string>
   <string name="buttonTextPostClick">I have been clicked !</string>
   <string name="action_settings">Settings</string>
</resources>

Next, we update MainActivity.xml to refer to the strings defined in strings.xml:

<RelativeLayout xmlns:android=
      "http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android:text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/buttonTextPreClick"
      android:id="@+id/button"
      android:layout_below="@+id/textView"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView"
      android:layout_marginTop="70dp" />

</RelativeLayout>

You can see from the highlighted section above that we have specified the resource for the button to be a string named buttonTextPreClick.

Next, we add a click event handler. In this click event handler, we will swap the string resource for the button text to be buttonTextPostClick.

public void onbuttonClick(View v) {
   Button button = (Button)
   findViewById(R.id.button);
   button.setText(R.string.buttonTextPostClick);
}

Now, we need to wire up the click event handler to the button's onclick event.

Open MainActivity.xml and make the following change.

<RelativeLayout xmlns:android=
      "http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

   <TextView android:text="@string/hello_world"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/buttonTextPreClick"
      android:id="@+id/button"
      android:layout_below="@+id/textView"
      android:layout_toRightOf="@+id/textView"
      android:layout_toEndOf="@+id/textView"
      android:layout_marginTop="70dp"
      android:onClick="onbuttonClick" />

</RelativeLayout>

Our application is now complete.

When we launch the application, you should see what's in Figure 7. This illustrates the resource for button text that has been picked up was the integer format as we declared initially.

Res7
Figure 7: The initial text sent to the button

When we click the button, the text changes. This change indicates that we exercised a programmatic way to pick up the resource.

Res8
Figure 8: The subsequent text assures that the button has been pressed

We have shown how to access resources in two different ways.

Summary

In this article, we learned how to access resources in Android applications. I hope you have found this information useful. You can download the sample code from the download link at the end of this article.

About the Author

Vipul Patel is a technology geek based in Seattle. He can be reached at vipul.patel@hotmail.com. You can visit his LinkedIn profile at https://www.linkedin.com/pub/vipul-patel/6/675/508.


Tags: Android, android apps, AAPT, android studio, Java resources, Android Asset Packaging Tool




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel