2

I'm working on an app/game that should switch between portrait and reversePortrait in multiplayer mode. (Mainly because each player might use the keyboard on his/her turn while the device is fixed on the table between them.)

My app works fine when the auto-rotate is ON.. But the reversePortrait orientation is NEVER achievable when the device auto-rotate is OFF!

What I did so far is that I've set the orientation in the Manifest file:

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|keyboardHidden"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

then I change the orientation programmatically in runtime by calling this method when needed:

public void rotateScreen(boolean reverse){    
    if (reverse) {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
    }
    else{
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

But that still doesn't force to reversePortrait orientation if the device auto-rotation is OFF.

I also tried theonConfigurationChanged() method, but it didn't work as well. I think it only gets called AFTER changing the orientation already, not before!

I even tried screenOrientation="reversePortrait" in the Manifest file, but even that is ineffective when auto-rotation is OFF.

Aleksandar G
  • 1,163
  • 2
  • 20
  • 25
M. Sherbeeny
  • 109
  • 2
  • 12
  • You could add a check to your `rotateScreenMethod()` so that it checks `Settings.System.ACCELEROMETER_ROTATION`, and set it if it isn't. See this answer for more information: http://stackoverflow.com/a/4909079/2891462. – user2891462 Apr 27 '16 at 14:37

2 Answers2

0

I don't know what exactly you want to achieve. Call your method in OnConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    rotateScreen(true);
    super.onConfigurationChanged(newConfig);
}

but here once first launch the screen is reversed but you can apply this as per your requirements.

Please provide more details what you want to achive

MobDev
  • 464
  • 4
  • 14
  • I tried that method too... it didn't work.. I think it's never called when the device auto-rotation is OFF... what I want to achieve is to be able to set the screen rotation to reverse portrait even if the device auto-rotation is OFF. – M. Sherbeeny Apr 26 '16 at 06:14
  • what exactly you want to achieve ? – MobDev Apr 26 '16 at 06:49
  • I want to achieve this: setting the screen rotation to reverse portrait even if the device auto-rotation is OFF. – M. Sherbeeny Apr 26 '16 at 06:57
  • so whenever screen is rotated to landscape mode you want it to be in reversePotrait. is this you to achieve ? – MobDev Apr 26 '16 at 07:03
  • No! Do you know that android phones become in a locked portrait mode when the user turns auto-rotatation off? my app should switch between portrait and reverse portrait based on the player turn (player 1 in portrait and player 2 in reverse portrait)... the app works fine when auto-rotation is ON.. but when auto-rotation is OFF, reverse portrait doesn't happen, it stays in portrait... I want to force reverse portrait whenever needed even if auto-rotation is OFF... I have updated the question, please, revise it. – M. Sherbeeny Apr 26 '16 at 17:19
  • Hey see if this links helps you http://stackoverflow.com/questions/4908048/enable-and-disable-auto-rotate-programatically – MobDev Apr 27 '16 at 07:01
0

After lots of researching, seems that the only way to override the device locked orientation and set the screen to a reverse portrait orientation is to force-enabling the device auto-rotate.

first, must add this permission in the manifest file above the tag:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

then, define something like that in the mainActivity:

public boolean enableAutoRotate(){

        boolean isAutoRotate = android.provider.Settings.System
                .getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1) == 1;

        if (!isAutoRotate){
            try {
                android.provider.Settings.System
                        .putInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1);

            } catch (Exception e){
                e.printStackTrace();
            }
        }

        isAutoRotate = android.provider.Settings.System
                .getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 1) == 1;

        return isAutoRotate;
}

The try and catch is important to prevent the app from crashing if the user didn't give or removed the permission.

then, set the orientation when needed using something like this:

public void rotateScreen(boolean reverse){

        if (enableAutoRotate()){
            if (reverse) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

            }

            else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }
        }
    }

rotateScreen method can also be boolean instead of void, if needed.

M. Sherbeeny
  • 109
  • 2
  • 12