Different resource files for different orientations



The string(s) in res/values-land/strings.xml will be used when the app is in landscape orientation. The string(s) in res/values/strings.xml will be used when the app is in all other orientations, or when a string is not found in res/values-land/strings.xml. We could have had a directory named res/values-port, but a catch-all directory named res/values is safer. See Providing Alternative Resources.

Source code in Land.zip

  1. LandActivity.java: untouched.
  2. res/layout/main.xml: untouched.
  3. res/values/strings.xml
  4. res/values-land/strings.xml
  5. AndroidManifest.xml: untouched.

Create the project

Create a new folder named values-land in the res folder. In the Package Explorer, right-click on res and select
New → Folder
Name the new folder values-land.

Create a new file named strings.xml in the values-land folder. In the Package Explorer, right-click on values-land and select
New → File
Name the new file strings.xml.

Things to try

  1. Use a different color for portrait and landscape. Create the files res/values/colors.xml and res/values-land/colors.xml containing a color element inside of a resources element. See color resources.

  2. Use a different font size for portrait and landscape. See dimension resources.

  3. Use a different image file for portarit and landscape. We already have a folder named res/drawable-hdpi. Create another folder named res/drawable-land-hdpi (not res/drawable-hdpi-land; see Providing Alternative Resources). Save this image (Leonardo da Vinci’s Mona Lisa) on your desktop as a file named myimage.jpg and then drag the file into drawable-hdpi.
    Select how files hsould be imported into the project.
    • Copy files.
    OK
    Then remove the file from your desktop. Similarly, save this image (Leonardo da Vinci’s The Last Supper) as a file named myimage.jpg in res/drawable-land-hdpi. Add the following element to the LinearLayout in main.xml immediately after the TextView.
    	<ImageView
    		android:layout_height="wrap_content"
    		android:layout_width="wrap_content"
    		android:src="@drawable/myimage"
    	/>
    
    You can add the android:gravity="center" attribute to the LinearLayout, and the attribute android:gravity="center" to the TextView.