Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Force Screen Orientation on Android
Qt 6.11 is out! See what's new in the release blog

Force Screen Orientation on Android

Scheduled Pinned Locked Moved Solved Mobile and Embedded
5 Posts 3 Posters 4.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matteo Vigni
    wrote on last edited by
    #1

    Hello,
    I am currently developing an Android application. The application is, for most of the screens, meant to be used in portrait orientation, but there are a couple of screens that require the landscape orientation. What I would like to achieve is the application to modify the screen orientation base on the screen that is displayed. Kinda like what the youtube app does when you switch to full screen.
    Is it there any way to achieve this behavior? If yes, which is the best way?
    Thanks everybody.

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      For the whole app beginning from the startup see this.

      If you want to change it dynamically during runtime i am afraid you need to do so via JNI. See this. (setRequestedOrientation on the Activity)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Matteo Vigni
        wrote on last edited by
        #3

        Thanks raven for the reply. Yes I did already see the post about the AndroidManifest.xml but since the app will have to rotate at runtime I will have to follow the JNI way.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          seyed
          wrote on last edited by seyed
          #4

          It's not so challenging. Use JNI as following simple implementation:

          void AndroidHelper::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);
              }
          }
          
          

          Don't forget to edit .pro file and add

          android {
              QT += androidextras
              SOURCES += androidhelper.cpp #my android specific cpp file
              HEADERS += androidhelper.h   #my android specific header file
          }
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            Matteo Vigni
            wrote on last edited by
            #5

            Thanks seyed, your solution is working, but i had to modify it slightly as it was giving me a compile error on this line

            QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;");
            

            So I changed it with this:

            QAndroidJniObject activity = QtAndroid::androidActivity();
            

            And it is working perfectly
            Thanks everybody for the help

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved