Intro to Android

Qualifiers

To set up alternative layouts and resources to be resolved at runtime, you use resource qualifiers.

Qualifiers cover things like screen size (width and height), languages and regions, aspect ration, orientation, and OS version among others.

Check out the complete listing of qualifier options as well as detailed documentation on how resources are selected: http://developer.android.com/guide/topics/resources/providing-resources.html

Folder structure

In this example res/ folder, we have a number of directories. Qualified folders can be recognized by the dash in their name and tells Android to use specific resources for specific instances.

res/
    drawable/
    values-sw600dp/
    values-fr/
    layout/
    layout-land/

-fr stands for French, -sw600dp means smallest width of the device is 600dp and higher, and -land is where the landscape layouts live. You could combine several qualifiers into one folder by using a dash between each qualifier.

Resolution

There's a bit of Android magic that selects the best version of your view or resource for the current device. If you set up the parallel folder structure to support different configurations, Android will handle the rest. Android chooses the best resources at runtime based on the device that is hosting the application.

Android tries to match with the most specific resource qualifiers first and then falls back to more generic options. If none of the resource qualifiers in your application match the host device, the basic values specified in layout/, values/, and drawable/ will be used.

Make sure to have a sane default! If you define something in a specific qualified folder, such as an id, and then run the app on a device with different specifications, it will crash because it won't be able to find the default value at runtime.

You might not catch this during development since you are likely using the same device. It's good practice to vary your test devices and testing situations to explore unhappy paths.

Naming

In the case of layouts, if you want to override the view for a specific configuration, you will need to create another version of the view in a qualified folder, but you use the same name for both files. That's how the resource qualification system works!

In the case of strings, dimens, drawables, and other resources, make sure to use the same name in the default and qualified paths to override the value for your new configuration.

Example: Internationalization

In values/strings.xml I have strings for a welcome screen.

<resources>
    <string name="WELCOME_TEXT">Hello Dutch Mobile Conference!</string>
    <string name="ABOUT_TEXT">Programmed by Corey Leigh Latislaw</string>
</resources>

In values-nl/strings.xml I defined the same name, but put the file in the qualified structure and changed the text.

<resources>
    <string name="WELCOME_TEXT">Hallo Dutch Mobile Conference!</string>
    <string name="ABOUT_TEXT">Geprogrammeerde door Corey Leigh Latislaw</string>
</resources>