Dynamically Change Screen Orientation on Android

Sorry For my late reply but it is not what I need.
This setting can not change the really screen Orientation.It can only give me this result:

190407-0.png

190408-1.jpg

the screen Orientation is still portrait.

sorry for my poor English.
In my game I want to achieve a function that can Dynamically change screen orientation.
for example:I want switch screen orientation to landscape when player move into some room and switch it back to Portrait when player leave those room.
How can I Do this?

Hello JackTM,

You should set the camera aspect ratio. If your screen ratio is 16:9 then camera ratio 0.5625 for landscape and 1.7777 for portrait. So, PortraitRatio= ScreenHeight / ScreenWidth and LandscapeRatio= ScreenWidth/ScreenHeight.

200275-ratio.png

1 Like

I Find the answer by myself.
It use JNI to call Java method “setRequestedOrientation” to done this.
Here is the Code:

#include "Android/AndroidJNI.h"  
#include "Android/AndroidApplication.h"  

JNIEnv* JE = FAndroidApplication::GetJavaEnv();  
jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, "setRequestedOrientation", "(I)V", false);  
FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID,0);
1 Like

Hi. I was investigating how to do what you mention. I see you did it. I only have one doubt? You do this in a new empty class or where you implement the code that you left here. Thank you and sorry for the inconvenience

For myself,I have an BPFuncLib class, It store some function and can be called in blueprint.The function is very simple,so you can just make a static function in your utility class and add them into it. call it directly, it doesn’t need any context because the GameActivity will always valid.
By the way: There have a paramter that control the target orientation that you want to change to, typically it use a variable like SCREEN_ORIENTATION_LANDSCAPE,but for convenience I use it’s value directly ,you can find the value of them in AndroidSDK and make them to an enum, It will make you fell better than use a magic number.
And, AndroidJNI.h may not valid for windows, add a macro like #if defined(PLATFORM_ANDROID)&&PLATFORM_ANDROID or your code may not be able to compile for editor.

muchas gracias por tu pronta respuesta. Una pregunta mas. De donde has bajado “AndroidJNI.h”?

It’s provided by Unreal Engine, add Launch module dependence will allow you use it.

I think I’m not doing my class well as you indicated.
what I did was make a new class where I define a function ToggleOrientation (). My cpp is composed as follows.

#include "funcLib.h"
#include "Android / AndroidJNI.h"
#include "Android / AndroidApplication.h"
.......
void AfuncLib :: ToggleOrientation ()
{
#if PLATFORM_ANDROID || PLATFORM_IOS
        JNIEnv * JE = FAndroidApplication :: GetJavaEnv ();
jmethodID SetOrientationID = FJavaWrapper :: FindMethod (JE, FJavaWrapper :: GameActivityClassID, "setRequestedOrientation", "(I) V", false);
FJavaWrapper :: CallVoidMethod (JE, FJavaWrapper :: GameActivityThis, SetOrientationID, 0);
#else
#endif
}

Where it says ‘Android / AndroidJNI.h’: No such file or directory

Again, thanks for the help. Sorry for the inconvenience

Open your build.cs,add “Launch” item to PublicDependencyModuleNames. It’s not about C++, it’s use for UBT, read some docs and you will learn more about it.

open your build.cs, add “Launch” to PublicDependencyModuleNames. It’s not about C++, It’s use for UBT. Read some docs you will learn more about it.

And,#include should also inside of #if PLATFORM_ANDROID macro.

Thanks for all the help Jack Myth.
moved the
#include “Android / AndroidJNI.h”
#include “Android / AndroidApplication.h”
in the #if PLATFORM_ANDROID || PLATFORM_IOS as you told me and in my builds.cs add what you indicated to me.

PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "Launch","InputCore" });

For some reason I still can not compile apparently when adding “Launch” to the build.cs an error that says that it fails to produce DebugGame.dll. If I remove the “Launch” I can compile in visual but when I try to build for android from the engine it tells me that Android / AndroidJNI.h does not fount

Launch module is not valid for windows, if you add this you will failed to compile for windows. Only add the dependency for android by using “if(Target.IsInPlatformGroup(UnrealPlatformGroup.Android))” in build.cs.

All in all, those code and module should always be ignored for all platforms except Android. Because they are platform-related. No other platform use AndroidJNI.

Thank you, visual and let me compile doing what you told me. but now the engine when wanting to compile for android generates several errors that say the same thing.
D: / Program Files / Epic Games / UE_4.20 / Engine / Source / Runtime / Launch / Public \ Android / AndroidJNI.h (37,19): error: static data member ‘AndroidThunkJava_KeepScreenOn’ not allowed in local class ‘FJavaWrapper’

Thanks again for all the help

My build.cs
public Math_Maze(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });

        if (Target.Platform == UnrealTargetPlatform.IOS)
        {
            PublicDependencyModuleNames.AddRange(new string[] { "Launch" });
        }
        else if (Target.Platform == UnrealTargetPlatform.Android)
        {
            PublicDependencyModuleNames.AddRange(new string[] { "Launch" });
        }


            PrivateDependencyModuleNames.AddRange(new string[] {  });

    

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using online features
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
    }

I guess your cpp have.some mistake.
Did You Put #include Inside Of the Defination?

Yes. My cpp

    #include "funcLib.h"
    
    void AfuncLib::ToggleOrientation()
    {
    #if PLATFORM_ANDROID || PLATFORM_IOS 
    #include "Android/AndroidJNI.h"
    #include "Android/AndroidApplication.h"
    
    	JNIEnv* JE = FAndroidApplication::GetJavaEnv();
    	jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, "setRequestedOrientation", "(I)V", false);
    	FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID, 0);
    #else 
    	
    #endif 
    }

#if PLATFORM_ANDROID || PLATFORM_IOS
#include “Android/AndroidJNI.h”
#include “Android/AndroidApplication.h”
#endif
void AfuncLib::ToggleOrientation()
{
#if PLATFORM_ANDROID || PLATFORM_IOS
JNIEnv* JE = FAndroidApplication::GetJavaEnv();
jmethodID SetOrientationID = FJavaWrapper::FindMethod(JE, FJavaWrapper::GameActivityClassID, “setRequestedOrientation”, “(I)V”, false);
FJavaWrapper::CallVoidMethod(JE, FJavaWrapper::GameActivityThis, SetOrientationID, 0);
#else

 #endif 
 }

thank you. It already compiles without problems.
A thousand thanks for all the help