How Android Finds The Best Matching Resources

Directory

Resource Types

res/anim/

XML files that are compiled into frame by frame animation or tweened animation objects

res/drawable/

.png, .9.png, .jpg files that are compiled into the following Drawable resource subtypes:

• 9-patches (resizable bitmaps) To get a resource of this type, use mContext.getResources().getDrawable(R.drawable. imageld) Note: Image resources placed in here may be automatically optimized with lossless image compression by the aapt tool. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette. This will result in an image of equal quality but which requires less memory. So be aware that the image binaries placed in this directory can change during the build. If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in the res/raw/ folder instead, where they will not be optimized.

res/layout/

XML files that are compiled into screen layouts (or part of a screen). See Declaring Layout.

res/values/

XML files that can be compiled into many kinds of resource. Note: Unlike the other res/ folders, this one can hold any number of files that hold descriptions of resources to create rather than the resources themselves. The XML element types control where

these resources are placed under the R class. While the files can be named anything, these are the typical files in this folder (the convention is to name the file after the type of elements defined within):

• arrays.xml to define arrays

• colors.xml to define color drawables and color string values.

Use Resources.getDrawable() and Resources.getColor(), respectively, to get these resources.

• dimens.xml to define dimension value. Use Resources.getDimension() to get these resources.

• strings.xml to define string values (use either Resources.getString or preferably Resources.getText() to get these resources. getText() will retain any rich text styling which is usually desirable for UI strings.

• styles.xml to define style objects.

res/xml/

Arbitrary XML files that are compiled and can be read at run time by calling Resources.getXML().

res/raw/

Arbitrary files to copy directly to the device. They are added uncompiled to the compressed file that your application build produces. To use these resources in your application, call Resources.openRawResource() with the resource ID, which is R.raw. somefilename.

ttHBS

WHft^StfflSl:

package com.android.samples; public final class R {

public static final class string {

public static final int greeting=0x0204000e;

public static final int start_button_text=0x02040001;

public static final int submit_button_text=0x02040008;

public static final int main_screen_title=0x0204000a;

public static final class layout {

public static final int start_screen=0x02070000;

public static final int new_user_pane=0x02070001;

public static final int select_user_list=0x02070002;

public static final class drawable {

public static final int company_logo=0x02020005; public static final int smiling_cat=0x02020006; public static final int yellow_fade_background=0x02020007; public static final int stretch_button_1=0x02020008;

^Iffl^MW^^: R.resource_type.resource_name android.R.resource_type.resource_name

// Load a background for the current screen from a drawable resource.

this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image

// WRONG Sending a string resource reference into a // method that expects a string. this.getWindow().setTitle(R.string.main_title);

// RIGHT Need to get the title from the Resources wrapper. this.getWindow().setTitle(Resources.getText(R.string.main_title));

// Load a custom layout for the current screen. setContentView(R.layout.main_screen);

// Set a slide in animation for a ViewFlipper object. mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in));

// Set the text on a TextView object.

TextView msgTextView = (TextView)findViewByID(R.id.msg); msgTextView.setText(R.string.hello_message);

£ XML MttSIWfifiWKt^flt^fflo <?xml version="1.0" encoding="utf-8"?> <EditText id="text" 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="Hello, World!" />

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

public class MyActivity extends Activity {

public void onStart() {

requestScreenFeatures(FEATURE_BADGE_IMAGE); super.onStart();

setBadgeResource(android.R.drawable.sym_def_app_icon);

icono public class MyActivity extends Activity {

public void onStart()

super.onStart();

setTheme(android.R.style.Theme_Black);

MyApp/ res/ values-en/

strings.xml values-fr/ strings.xml

Qualifier

Values

MCC and MNC

The mobile country code optionally followed by mobile network code from the SIM in the device. For example mcc310 (U.S. on any carrier); mcc310-mnc004 (U.S., Verizon brand); mcc208-mnc00 (France, Orange brand); mcc234-mnc00 (U.K., BT brand).

If the device uses a radio connection (GSM phone), the MCC will come from the SIM, and the MNC will come from the network to which the device is attached. You might sometimes use the MCC alone, for example to include country-specific legal resources in your application. If your application specifies resources for a MCC/MNC combination, those resources can only be used if both the MCC and the MNC match.

Language and region

The two letter ISO 639-1 language code optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r"). For example fr, en-rUS, fr-rFR, es-rES. The codes are not case-sensitive; the r prefix is used to distinguish the region portion. You cannot specify a region alone, but you can specify a language alone, for example en, fr, es.

Screen dimensions

small, normal, large

Specify that the resource is for a particular class of screen. The meanings of these are:

• Normal screens are based on the traditional Android HVGA medium density screen. A screen is considered to be normal if it is at least this size (independent of density) and not large. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density.

• Small screens are based on the space

}

available on a QVGA low density screen. Considering a portrait HVGA display, this has the same available width but less height -- it is 3:4 vs. HVGA's 2:3 aspect ratio. Examples are QVGA low density and VGA high density.

• Large screens are based on the space available on a VGA medium density screen. Such a screen has significantly more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium density screens.

Wider/taller screens

long, notlong

Specify that the resource is for a taller/wider than traditional screen. This is based purely on the aspect ration of the screen: QVGA, HVGA, and VGA are notlong; WQVGA, WVGA, FWVGA are long. Note that long may mean either wide or tall, depending on the current orientation.

Screen orientation

port, land, square

Specifies that the resource is for a screen that is tall (port) or wide (land); square is not currently used.

Screen pixel density

ldpi, mdpi, hdpi, nodpi

Specifies the screen density the resource is defined for. The medium density of traditional HVGA screens (mdpi) is defined to be approximately 160dpi; low density (ldpi) is 120, and high density (hdpi) is 240. There is thus a 4:3 scaling factor between each density, so a 9x9 bitmap in ldpi would be 12x12 is mdpi and 16x16 in hdpi. The special nodpi density can be used with bitmap resources to prevent them from being scaled at load time to match the device density.

When Android selects which resource files to use, it handles screen density differently than the other qualifiers. In step 1 of How Android finds the best matching directory (below), screen density is always considered to be a match. In step 4, if the qualifier being considered is screen density, Android will select the best final match at that point, without any need to move on to step 5.

You can also specify explicit densities like 92dpi or 108dpi, but these are not fully supported by the system so should not be used.

Touchscreen type

notouch, stylus, finger

Whether the keyboard is available to the user

keysexposed, keyshidden, keyssoft If your application has specific resources that should only be used with a soft keyboard, use the keyssoft value. If no keyssoft resources are available

(onlykeysexposed and keyshidden) and the device shows a soft keyboard, the system will use keysexposed resources.

Primary text input method

nokeys, qwerty, 12key

Whether the navigation keys are available to the user

navexposed, navhidden

If the hardware's navigation keys are currently available to the user, the navexposed resources will be used; if they are not available (such as behind a closed lid), navhidden will be used.

Primary non-

nonav, dpad, trackball, wheel

touchscreen navigation method

Screen dimensions

320x240, 640x480, etc. The larger dimension must be specified first. This configuration is deprecated and should not be used; use instead screen dimension, wider/taller screens, and screen orientation described above.

SDK version

The SDK version supported by the device, for example v3. The Android 1.0 SDK is v1, the 1.1 SDK is v2, and the 1.5 SDK is v3.

(Minor version)

(You cannot currently specify minor version. It is always set to 0.)

All resources will be referenced in code or resource reference syntax by their simple, undecorated names. So if a resource were named this:

MyApp/res/drawable-port-mdpi/myimage.png It would be referenced as this: R.drawable.myimage (code) @drawable/myimage (XML)

ANDROID

0SWo

TffifiW^W

0 0

Responses

  • Tuomo
    How android finds the best matching resources?
    10 months ago

Post a comment