Referencing Resources within Resources

You can also use resource references as attribute values in other XML resources.

This is particularly useful for layouts and styles, letting you create specialized variations on themes and localized strings and graphics. It's also a useful way to support different images and spacing for a layout to ensure that it's optimized for different screen sizes and resolutions.

To reference one resource from another use @ notation, as shown in the following snippet.

attribute="@[packagename:]resourcetype/resourceidentifier"

Android will assume you' re using a resource from the same package, so you only need to fully qualify the package name if you' re using a resource from a different package.

Listing 3-4 shows a layout that uses color, dimension, and string resources.

LISTING 3-4: Using resources in a layout

Available for download on Wrox.com

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="@dimen/standard_border">

<EditText android:id="@+id/myEditText"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/stop_message"

android:textColor="@color/opaque_blue"

</LinearLayout>

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment