Changing Orientation Of A Particular Page In Android
Solution 1:
You don't need to call it through Java code. You can directly call it from C++ using JNI as follow:
void MyAndroidHelperClass::setScreenOrientation(int orientation)
{
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
if ( activity.isValid() )
{
activity.callMethod<void>
("setRequestedOrientation"// method name
, "(I)V"// signature
, orientation);
}
}
Solution 2:
Screen orientation on Android can be changed using setRequestedOrientation
Java function so you should call a Java function from your app. To run Java code in your Qt Android application you should use the Qt Android Extras module which contains additional functionality for development on Android.
You can use JNI to call a Java function from C/C++ or callback a C/C++ function from Java.
Here you could have it in a static Java method like :
package com.MyApp;
publicclassOrientationChanger
{
publicstaticintchange(int n)
{
switch(n)
{
case0:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case1:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
}
}
}
First you need to add this to your .pro file :
QT += androidextras
And Include the relevant header file :
#include<QAndroidJniObject>
You can then call this static Java function from your C++ code.
To change the orientation to landscape mode :
bool retVal = QAndroidJniObject::callStaticMethod<jint>
("com/MyApp/OrientationChanger"// class name
, "change"// method name
, "(I)I"// signature
, 0);
To change the orientation to portrait mode :
bool retVal = QAndroidJniObject::callStaticMethod<jint>
("com/MyApp/OrientationChanger"// class name
, "change"// method name
, "(I)I"// signature
, 1);
Post a Comment for "Changing Orientation Of A Particular Page In Android"