SECRETARÍA GENERAL:
1.1.7 Las relaciones del SERE con las autoridades francesas
USING <INCLUDE> FOR SMALL CHANGES
The <include> tag is a fantastic way to pull out small portions of your screen that you’d like to tweak and lay out separately. This is the scalpel method. You cut out only the portions you want to render differently, you split them into folder-separated layouts, and you’re finished. Which, in this case, is a perfect way to excise the buttons and have them render differently depending on the screen orientation.
Here’s how to do exactly that.
1. Create a new button_layout.xml file in res/layout-land/. This is the file into which we’ll put the landscape-specific layout.
2. Add a LinearLayout and two Buttons in the new button_layout.xml file, and place them next to each other in the new horizontal linear layout. Here’s the final contents of my /layout-land/button_layout.xml.
ptg7794906
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”horizontal”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginBottom=”60dp”
android:layout_alignParentBottom=”true”
>
<Button
android:padding=”15dp”
android:gravity=”center”
android:id=”@+id/yes_button”
android:layout_marginLeft=”20dp”
android:layout_marginRight=”5dp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/yes_button_text”
android:layout_weight=”50”
/>
<Button
android:padding=”15dp”
android:gravity=”center”
android:id=”@+id/no_button”
android:layout_marginLeft=”5dp”
android:layout_marginRight=”20dp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
UNCOVERING THE SECRETS OF THE RES/ FOLDER 173
ptg7794906 android:text=”@string/no_button_text”
android:layout_weight=”50”
/>
</LinearLayout>
As you can see, I’ve created a horizontal layout with two buttons to be used in landscape mode.
Don’t forget about the portrait layout. If we’re going to include a layout, it’s got to exist for both the landscape configuration and the default configuration.
3. Create a new button_layout.xml file in /res/layout/ (or you could add it as /res/layout-port/button_layout.xml). I’ve just copied the original buttons’ code and pasted it into a new RelativeLayout:
<?xml version=”1.0” encoding=”utf-8”?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_marginBottom=”60dp”
android:layout_width=”fill_parent”
android:layout_height=”100dp”
android:layout_alignParentBottom=”true”
>
<Button
android:padding=”15dp”
android:gravity=”center”
android:id=”@+id/yes_button”
android:layout_marginLeft=”30dp”
android:layout_marginRight=”30dp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
ptg7794906 android:layout_above=”@+id/no_button”
android:text=”@string/yes_button_text”
/>
<Button
android:padding=”15dp”
android:gravity=”center”
android:id=”@+id/no_button”
android:layout_marginLeft=”30dp”
android:layout_marginRight=”30dp”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/no_button_text”
android:layout_alignParentBottom=”true”
/>
</RelativeLayout>
At this point, you’ve created two layouts: one for portrait and one for landscape.
4. You can now modify your original XML with an include, like so:
<RelativeLayout>
<!--Text view and relative layout params omitted-->
<include layout=”@layout/button_layout”
android:id=”@+id/button_holder”
/>
</RelativeLayout>
With the <include> tag in place (instead of a single button definition), Android will grab the button_layout.xml file that corresponds to the screen configura-tion. If it can’t find one, it will default back to what’s in /res/layout.
UNCOVERING THE SECRETS OF THE RES/ FOLDER 175
ptg7794906 Now, with this new code, the landscape mode looks much better (Figure 7.3).
Are there things that could be improved? Sure! Now, however, you know how to specify that parts of your user interface should change as the screen’s hardware configuration changes.
MERGING
You don’t have to wrap your excised views in a new ViewGroup (RelativeLayout in the previous example) for them to be included (as I did here). If you don’t want to add another layout to the mix but would like to bring in a series of views from other XML files, simply wrap them in a <merge> tag.
<merge xmlns:android=”http://schemas.android.com/apk/res/android”>
<!-- views go here -->
</merge>
This will allow you to include views without adding another layout to your view hierarchy.
FIGURE 7.3 No awards for design, but much better.
ptg7794906 WHAT CAN YOU DO BEYOND LANDSCAPE?
Lots. You can add suffixes to layout folders to account for just about everything.
Here are a few I use on a regular basis.
䊏 layout-small, layout-normal, layout-large, layout-xlarge
The size modifier accounts for the physical size of the screen. Devices that would use the small layout folder are typically very old, or very strange pieces of hardware, at least until Android powered watches become popular.
Most modern phones fit the layout-normal category, while many tablets are considered xlarge. Google keeps a great breakdown of all the various screen configurations at http://developer.android.com/resources/dashboard/
screens.html.
䊏 layout-ldpi, layout-mdpi, layout-hdpi, layout-xhdpi
The dpi, or dots per inch, of the device is a measurement of screen density.
Screens with high densities (240 dpi) would pull from the layout folder layout-hdpi.
䊏 layout-large-hdpi-land
You can also mix and match the suffixes. This suffix would be used for phones that have large screens and high resolution and that are in land-scape mode. Get creative, but remember that just because you can get very specific about screen configurations, it doesn’t mean you should.