Using Alternate Resources

As a test, try switching the emulator to landscape mode (Ctrl+F1^ or the Q or [9 key on the keypad). Oops! The Exit button runs off the bottom of the screen (see Figure 3.5). How do we fix that?

You could try to adjust the layout so that it works with all orientations. Unfortunately, that's often not possible or leads to odd-looking screens. When that happens, you'll need to create a different layout for landscape mode. That's the approach we'll take here.

Create a file called res/layout-land/main.xml (note the -land suffix) that contains the following layout:

Download Sudokuv1/res/layout-land/main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""



android:layout_width= "fill_parent"


android:orientation= "horizontal" >

<LinearLayout android:orientation= "vertical" android:layout_height= "'wrap_content" android:layout_width= "fill_parent" android:layout_gravity="center" android:paddingLeft="20dip" android:paddingRight= "20dip"> <TextView android:text="@string/main_title" android:layout_height= "'wrap_content" android:layout_width= "'wrap_content" android:layout_gravity= "'center" android:layout_marginBottom="20dip" android:textSize= "24.5sp" /> <TableLayout android:layout_height= "'wrap_content" android:layout_width= "'wrap_content" android:layout_gravity= "'center" android:stretchColumns="*■"> <TableRow> <Button android:id="@+id/continue_button" android:text="@string/continue_label" /> <Button android:id= "@+id/new_button" android:text="@string/new_game_label" /> </TableRow> <TableRow> <Button android:id="@+id/about_button" android:text="@string/about_label" /> <Button android:id="@+id/exit_button" android:text="@string/exit_label" /> </TableRow> </TableLayout> </LinearLayout> </LinearLayout>

IM© 1:37 AM


Android Sudoku

Figure 3.6: Using a landscape-specific layout lets us see all the buttons.

This uses a TableLayout to create two columns of buttons. Now run the program again (see Figure 3.6). Even in landscape mode, all the buttons are visible.

You can use resource suffixes to specify alternate versions of any resources, not just the layout. For example, you can use them to provide localized text strings in different languages. Android's screen density support depends heavily on these resource suffixes (see Section 13.5, All Screens Great and Small, on page 267).

0 0

Post a comment