Search code examples
javaandroidemulationlandscape

Emulator does not use the landscape variant


So I have a very simple project with just a few images. I have created a landscape variation, and changed the position of the images.

The problem is, that when running the project on an emulator or device, the default landscape layout is used instead of the variant.

If its run while the emulator is in the landscape position, then it does use the variant, but messes up the portrait.

The only other thing that I've changed is the styles.xml to have:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

as the App Theme.

I'm fairly new to Android Dev. so it could be something fairly simple.

Any help is greatly appreciated.

Manifest:

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize|keyboardHidden" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

</manifest>

What it should look like:

https://i.sstatic.net/UXQCd.png

What it shouldn't look like:

https://i.sstatic.net/dS54W.png


Solution

  • I found a solution.

    This involves keeping the line:

    android:configChanges="orientation|screenSize|keyboardHidde‌​n" >
    

    In MainActivity.java just add a onConfigurationChanged function like so:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    
        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //retrieving resources for the variations
            setContentView(R.layout.activity_main);
    
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            //retrieving resources for the variations
            setContentView(R.layout.activity_main);
    
        }
    }
    

    That's the best solution I could come up with. There might be a better way, as this seems a bit redundant.