-1

My bare-bones test app sets the orientation to landscape on start up. It has a layout and layout-port directory. There is a simple layout file in each directory. They contain a textview with text set to either "Landscape" or "Portrait".

In my test I start the app on a tablet tipped into portrait. With this code, I would expect the layout from /layout/my_layout.xml to be used, but /layout-port/my_layout.xml is used instead. I've also tried making a /layout-land directory but still no luck.

From my main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Start app with tablet in portrait orientation
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    int o = this.getRequestedOrientation();
    // o is 0, indicating landscape. Display shifted to landscape
    setContentView(R.layout.my_layout);
            // layout-port/my_layout.xml is loaded. INCORRECT I think
    }

/layout/my_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000">


    <TextView
        android:id="@+id/tvLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Landscape"
        android:textSize="50sp" 
        android:textColor="#ffffffff"/>

</LinearLayout>

/layout-port/my_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000">


    <TextView
        android:id="@+id/tvLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Portrait"
        android:textSize="50sp" 
        android:textColor="#ffffffff"/>

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mytest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mytest.ActivityMain"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:windowSoftInputMode="stateHidden|adjustPan" >
          <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Mark Murphy
  • 1,580
  • 1
  • 16
  • 29

2 Answers2

1

Change /res/layout/my_layout.xml to /res/layout-land/my_layout.xml
Note: I added the suffix -land

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
0

Your have to remove option "orientation" in manifest:

android:configChanges="screenSize|orientation|keyboardHidden"

Because this option says you handle it by yourself...

Good explanation here:

Community
  • 1
  • 1
Tino
  • 299
  • 3
  • 15