# Dynamically Change Screen Orientation on Android

**URL:** <https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560>\
**Category:** Community & Industry Discussion\
**Tags:** unreal-engine, Android, screen, question\
**Created:** [June 10, 2017, 2:33am UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560 "2017-06-10T02:33:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [June 10, 2017, 2:33am UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/1 "2017-06-10T02:33:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SertacOgan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/sertacogan/32/970490_2.png) [@SertacOgan](https://forums.unrealengine.com/u/SertacOgan)\
**Post date:** [June 10, 2017, 7:50am UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/2 "2017-06-10T07:50:59Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/c/b/acbe716c930f4022d0537c494b4a1715923297ec.png)

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [June 11, 2017, 1:58am UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/3 "2017-06-11T01:58:22Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/e/d/bed2fc5b1f9c583f64a947ca7ff6577c89d1e759.png)

![190408-1.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/9/0/b905a9a3616bf4559646be4ad13f61f5145e37d7.jpeg)

the screen Orientation is still portrait.

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [June 19, 2017, 8:53am UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/4 "2017-06-19T08:53:26Z")

</div>

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);
```

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 8:25pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/5 "2019-01-14T20:25:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 8:46pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/6 "2019-01-14T20:46:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 9:08pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/7 "2019-01-14T21:08:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 9:19pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/8 "2019-01-14T21:19:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 9:36pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/9 "2019-01-14T21:36:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 9:36pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/10 "2019-01-14T21:36:38Z")

</div>

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.

> **[Build Tools](https://docs.unrealengine.com/4.27/en-US/ProductionPipelines/BuildTools/)**
>
> Tools to compile Unreal Engine and game projects.

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 9:44pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/11 "2019-01-14T21:44:09Z")

</div>

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.

> **[Build Tools](https://docs.unrealengine.com/4.27/en-US/ProductionPipelines/BuildTools/)**
>
> Tools to compile Unreal Engine and game projects.

And,#include should also inside of #if PLATFORM\_ANDROID macro.

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 10:06pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/12 "2019-01-14T22:06:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 10:22pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/13 "2019-01-14T22:22:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 10:31pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/14 "2019-01-14T22:31:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 10:33pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/15 "2019-01-14T22:33:20Z")

</div>

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’

 ![265379-captura.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/e/5/ce52bd7d5a201bec9a265f4130dece428419a7c6.png)

Thanks again for all the help

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 10:34pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/16 "2019-01-14T22:34:14Z")

</div>

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
    }
```

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 10:41pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/17 "2019-01-14T22:41:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 10:43pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/18 "2019-01-14T22:43:33Z")

</div>

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 
    }
```

---

<div class="post-metadata">

**Author:** ![Jack\_Myth](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Jack\_Myth](https://forums.unrealengine.com/u/Jack_Myth)\
**Post date:** [January 14, 2019, 10:45pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/19 "2019-01-14T22:45:41Z")

</div>

#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 
 }
```

---

<div class="post-metadata">

**Author:** ![Yvasquez](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Yvasquez](https://forums.unrealengine.com/u/Yvasquez)\
**Post date:** [January 14, 2019, 11:05pm UTC](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560/20 "2019-01-14T23:05:51Z")

</div>

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

[Next page](https://forums.unrealengine.com/t/dynamically-change-screen-orientation-on-android/398560.md?page=2)
