Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Developing Orientation-Aware Android Applications : Page 3

Even though users take device landscape changes for granted, these changes remain challenging for developers to program. This article outlines a few tricks that will save you time when programming for an Android device.


advertisement

Using the layout Folder

Apart from writing code, an easier way of customizing the UI based on screen orientation is to create a separate res/layout folder containing the XML files for the UI. To support landscape mode, you can create a new folder in the res folder and name it layout-land (representing landscape). Figure 8 shows that the new folder also contains the file main.xml.


Figure 8. Downloading Data: The layout-land folder contains the UI to be displayed in landscape mode.
 
Figure 9. Emulate 3G: You can also apply the –land name extension to the drawable folder.

Basically, the main.xml file contained within the layout folder defines the UI for the activity in portrait mode while the main.xml file in the layout-land folder defines the UI in landscape mode. The following shows the content of main.xml under the layout-land folder:



<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/button1" android:layout_width="150px" android:layout_height="60px" android:text="Button" android:layout_x="280px" android:layout_y="180px" /> </AbsoluteLayout>

Basically, the main.xml file contained within the layout folder defines the UI for the activity in portrait mode while the main.xml file in the layout-land folder defines the UI in landscape mode. The following shows the content of main.xml under the layout-land folder:

<?xml version="1.0" encoding="utf-8"?&glt; <AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" &glt; <Button android:id="@+id/button1" android:layout_width="150px" android:layout_height="60px" android:text="Button" android:layout_x="280px" android:layout_y="180px" /&glt; </AbsoluteLayout&glt;

Using this method, when the orientation of the device changes, Android will automatically load the appropriate XML file.

Besides using the –land name extension on the res/layout folder, you can also use it on the res/drawable folder. For example, the res/drawable-land folder shown in Figure 9 contains images that are designed to be displayed in landscape mode while those in the res/drawable folder are designed to be displayed in portrait mode.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap