Different layouts for landscape and portrait orientations in Android

June 29, 2012
By

In this article we will create an Android application which uses different layouts for landscape and portrait views of the device


1. Create an Android project namely “LandPortView”

New Android Project

Figure 1: New Android Project


2. Select Android build target

Select Android build target

Figure 2: Select Android build target


3. Enter application details

Enter Application Details

Figure 3: Enter Application Details


4. res/values/strings.xml


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

    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">LandPortViews</string>
    <string name="str_land">This is landscape view</string>
    <string name="str_port">This is portrait view</string>

</resources>


5. Create a new folder namely “layout-land” under the folder “res”


6. Create a layout file called main.xml in the folder “layout-land” and add the given below code


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/str_land"
    android:layout_gravity="center_horizontal"

/>

</LinearLayout>


7. Create a new folder namely “layout-port” under the folder “res”


8. Create a layout file called main.xml in the folder “layout-port” and add the given below code :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_port"
        android:layout_gravity="center_horizontal"
    />

</LinearLayout>


9. Application in execution

Application in Portrait view

Figure 4 : Application in Portrait view


10. Download


11. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , , ,

3 Responses to Different layouts for landscape and portrait orientations in Android

  1. Neha on September 22, 2012 at 5:01 pm

    Thank you so much for very nice and useful post!!

  2. jayshree on February 12, 2013 at 5:40 pm

    nice post sir…
    but when we change orientation from landscape to portrait the view doesnt change ,it leave landscape view only…

    If possible plz post suggestion for this issue.

    • george on February 13, 2013 at 4:08 pm

      Can you please recheck the issue? Because i am not able to find the issue.

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner