Android: preventing orientation changes programmatically

I use:

setRequestedOrientation(getResources().getConfiguration().orientation);

and then:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

it prevents orientation changes until the task is completed, but it only works on the portrait, when the application is on the landscape, it does not stop the orientation for the change.

Any suggestions?
OIM ~

+4
source share
2 answers

Everything turned out fine for me in all cases.

To fix the screen:

if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_0)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_90)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
if (getWindowManager().getDefaultDisplay().getRotation()== Surface.ROTATION_270)
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);

and then enable rotation again:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

OIM ~

+5
source

In the manifest file with your activity try

    android:screenOrientation="sensorLandscape"
    android:windowSoftInputMode="stateAlwaysHidden" 

Inside the application tag in the manifest file try

    android:hardwareAccelerated="true"
    android:largeHeap="true"
0
source

All Articles