Swiping your finger across the screen from top to bottom, we are at the top of the menu with buttons. For example: sound (change sound), screen rotation (enable or disable screen rotation), etc.


Is it possible to programmatically click on the "Rotate the screen" button, namely: to prohibit screen rotation?

In other words: how to programmatically prohibit the rotation, and at the end of the program to return to the position before starting. Those. if before the launch there was a landscape, then it will remain until the end of the application, and if there was a portrait, will it also remain until the end of the application?

Option with android: screenOrientation = "portrait" in the Manifesto not to offer. He does not solve this problem!

Thanks in advance...

    3 answers 3

    Answers with en-SO one and two

    1. We get two options: landscape and portrait

      int orientation=this.getResources().getConfiguration().orientation; if(orientation==Configuration.ORIENTATION_PORTRAIT){ //портрет } else{ //ландшафт } 
    2. We get 4 options with angles of rotation relative to the default value

       int rotation = getWindowManager().getDefaultDisplay().getRotation(); int angle = 0; switch (rotation) { case Surface.ROTATION_90: angle = -90; break; case Surface.ROTATION_180: angle = 180; break; case Surface.ROTATION_270: angle = 90; break; default: angle = 0; break; } 

    Now set the resulting orientation:

     setRequestedOrientation(/* сюда поместите одно из значений констант класса ActivityInfo */); 
    • Everything seems to be normal, but it does not determine a 180-degree turn when starting: case Surface.ROTATION_180: setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); break; - LEO
    • @ LEO, does not detect on all devices? - Yuriy SPb
    • Almost everyone. Standard launcher can not rotate the screen 180 degrees. - LEO
    • 2
      @ LEO, well, you probably have to use 0 degrees in this case and swear on the progers from Google) - Yuriy SPb
    • Let me explain why I am so 180 degrees. The fact is that I am left-handed (I do not have the wrist of my right hand), I do everything with my left hand, including driving a car too. And like me, not enough. Because Gadget makers basically have right-handed buttons, this is extremely inconvenient for me and, if possible, I use left-handed programs when you can flip the screen 180 degrees. - LEO

    Here is what I found in the documentation, and it works:

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED); 

    Unlock:

      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_USER); 

      All from the same en-SO:

       public class LockOrientation { Activity activity; public LockOrientation(Activity act) { this.activity = act; } @SuppressLint("InlinedApi") public void lock() { switch (activity.getResources().getConfiguration().orientation) { case Configuration.ORIENTATION_PORTRAIT: if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); if (rotation == android.view.Surface.ROTATION_90 || rotation == android.view.Surface.ROTATION_180) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } break; case Configuration.ORIENTATION_LANDSCAPE: if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); if (rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } break; } } public void unlock() { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } 

      }

      Call: lock: new LockOrientation(this).lock(); unlock: new LockOrientation(this).unlock();

      • The same code on ru-SO: tyk - Yuriy SPb
      • Next time I promise to refresh the page before writing - Oleg A
      • I didn’t quite understand what the page update was about) - YuriySPb
      • did not correctly read your message - Oleg A