Screen orientation not changing when rotating Android device

I’m currently using SDL2 version 2.0.5 for the Android port of my game, which works just fine, but I would like to upgrade to a newer SDL version.

However, I have the problem that automatic screen rotation does not work anymore when using a newer version of SDL2. It still rotates 180° from landscape mode to flipped landscape mode when turning the device by 180°, but it refuses to rotate from landscape to portrait mode or from portrait to landscape mode, but just stays in whatever orientation the game was started with (either landscape or portrait mode).

I’ve encountered this problem when using SDL2 versions 2.0.6 or 2.0.8 (newer versions 2.0.9 and 2.0.10 unfortunately refuse to compile on my development system).

I have tested (and reproduced) this odd behavior on Android 4.4 and Android 9.

I have the suspicion that it may have something to do with my “AndroidManifest.xml” file, but haven’t found a solution so far.

Any idea what might go wrong here?

Here’s my “AndroidManifest.xml” file:

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.artsoft.rocksndiamonds"
      android:versionCode="4010301"
      android:versionName="4.1.3.1"
      android:installLocation="auto">

    <application android:label="@string/app_name"
                 android:icon="@drawable/ic_launcher"
                 android:allowBackup="true"
                 android:debuggable="true"
                 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                 android:hardwareAccelerated="true">
        <activity android:name="RocksNDiamonds"
                  android:configChanges="keyboardHidden|orientation|screenSize"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <!-- Android 2.3.3 (min) / Android 4.2.2 (target) -->
    <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17" />

    <!-- OpenGL ES 2.0 -->
    <uses-feature android:glEsVersion="0x00020000" /> 

    <!-- Allow writing to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- Allow doing network operations -->
    <uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

Any hint or help would be highly appreciated.

I think you should try to solve this first ! try to fix compiling latest hg source.

I think you are right. It’s always best to do such tests with the latest SDL versions (both stable and from the repository).

So I’ll try again to compile the latest SDL code, and see if that helps…

Check out my tutorial how to build the latest SDL with android studio.
https://discourse.libsdl.org/t/building-sdl2-0-10-in-android-studio-3-4-2-in-windows-10

Regarding the screen orientation . Try adding the screenOrientation under activity:

<activity android:name="RocksNDiamonds"
                  android:configChanges="keyboardHidden|orientation|screenSize"
                  android:label="@string/app_name"
                  android:screenOrientation="sensorLandscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Read more about it here: https://developer.android.com/guide/topics/manifest/activity-element#screen

Another note: Do you really need the:

<!-- Allow writing to external storage -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Did you ever resolve this? I am experiencing the identical issue: currently using SDL 2.0.5 and rotations between landscape and portrait are working. Trying to update to SDL 2.0.10 and only 180-degree rotations are working, my app is stuck in portrait (or reverse portrait) orientation.

Of course I can’t be sure whether it’s the changes in SDL which are responsible or the different Android API level, but I need both portrait and landscape orientations to work.

Checking at Bugzilla I see there’s a bug which may be related to this issue. It’s well over a year old and shown as WAITING, what’s the status of this please?

Just to tie up loose ends, it was this bug. I’ve applied the suggested patch and rotation now works again; I hope this can be fixed properly in the next release of SDL2.

@DaSkuggo : Thank you for your reply! I’ve tried adding “screenOrientation” with various values (like “fullSensor”), but it does not change anything at all! :frowning:

Regarding the permissions: I am not sure if I really need it; what I want to do is write to the application-specific directories as returned by “SDL_AndroidGetExternalStoragePath()”, depending on “SDL_AndroidGetExternalStorageState()” returning bitmask “SDL_ANDROID_EXTERNAL_STORAGE_WRITE” set. Apparently it depends on the API level; also see: https://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE

@rtrussell : Thanks a lot for your comments – apparently I am not the only one who encountered this problem (and apparently it is not the fault of my build system)! :slight_smile:

I will try out this patch soon – which of them did you finally use? If I understand it right, not all of the suggested patches do the “right thing”?! :thinking:

I used Sylvain’s patch because I want the ‘full sensor’ mode of operation. But since I’m not using the SDL_HINT_ORIENTATIONS hint I didn’t really need to apply the full patch at all, I could simply have replaced the code with:

mSingleton.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

Sorry for the late reply. Took me quite some time to change my build system for the Android platform from ant to gradle, to finally be able to use the current SDL 2.0.12 for resolving this issue.

And the problem was still there with latest SDL 2.0.12 then.

But I finally found out what caused that problem: Somewhere between SDL 2.0.5 and 2.0.12, using the flag SDL_WINDOW_RESIZABLE when creating the SDL surface was required for working screen orientation changes on Android (together with the appropriate settings in the manifest file), even when using fullscreen mode!

So once I added SDL_WINDOW_RESIZABLE, everything was fine!

Maybe this could help somebody else using old SDL code and facing similar problems.

1 Like

Hey, does anyone knows if screen rotation hints also work on Android or are limited to iOS as stated in the documentation?

I ask because there’s a draft label on top in the docs and I don’t know what that means.

I read the Android source code and used them and they apparently work, just need to update the docs.

Sigh. On a quick scan of the What’s New list it looks as though the official online SDL2 documentation corresponds, roughly, to SDL 2.0.6. None of the functions added since are included, as far as I can see.