Search code examples
androidandroid-layoutlayoutandroid-orientation

how can I make sure the right layout from the right layout-folder is choosen when I change the orientation while the Activity is running?


I have different layouts for different screen-orientations in my application. The layouts are saved in layout-land and layout-port folders. When I start my Application Android choose the right orientation for the first Activity (Loadscreen in my case) but when I change the orientation of my phone after starting the first Activity Android doesn't change to the layout from the other orientation folder. How can I make sure the right layout get's selected if I change the orientation during the Activity runtime? Is there an OnOrientationChanged Method or something like that?

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ncss.tyfby">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Feeling"
        android:configChanges="keyboardHidden|orientation|screenSize">
    </activity>
    <receiver android:name=".SampleBootReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>
    <activity
        android:name=".Profile"
        android:configChanges="keyboardHidden|orientation|screenSize">
    </activity>
    <activity
        android:name=".Victorios"
        android:configChanges="keyboardHidden|orientation|screenSize">
    </activity>
    <activity
        android:name=".Breath"
        android:configChanges="keyboardHidden|orientation|screenSize">
    </activity>
    <activity
        android:name=".HighFive"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize">
    </activity>
    <activity
    android:name=".Settings"
    android:configChanges="keyboardHidden|orientation|screenSize">
    </activity>

    <activity
        android:name=".Loadscreen"
        android:configChanges="keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</manifest>

Solution

  • Remove this

    android:configChanges="keyboardHidden|orientation|screenSize">
    

    The default behaviour of android is to create a new object and inflate new layout files when orientation changes but this line of code prevents restarting of your activity when orientation changes and hence the layout in layout-land will be ignored.Read https://developer.android.com/guide/topics/resources/runtime-changes.html