Using XML layout references in Android

Android supports resource selectors based on qualifiers. For example to use a special layout file for activity_main.xml on a tablet in landscape mode you would typically define a layout file also called activity_main.xml in the layout-sw600-land folder.

Instead of using this approach you can use use XML layout references. With this approach you define a reference to a file for a defined layout name.

To use the approach for our example create the following refs.xml file in the values-sw600-land folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="layout" name="activity_main">@layout/activity_second</item>
</resources>

This file defines that if the layout with the name activity_main is used it should point to the real file activity_second.

So basically you have a second way of configuring which layout file should be used under which condition. I case you have a complex setup of different layout file you may want to use these references to avoid duplication of the layout files in different folders.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of vogella.com. With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android, Other and tagged . Bookmark the permalink.