Search code examples
androidandroid-orientation

reverse portrait not working in android


As the title says when calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

or declaring in the manifest

android:screenOrientation="reversePortrait"

Doesn't rotate the screen at all. When I try to use reverse landscape it works fine. This doesn't make nay sense. Did I do something wrong? Thanks in advance.


Solution

  • Try this code.

    public class MainActivity extends AppCompatActivity {
    
        @Override protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);// turn off rotation according to accelerometer
            Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_180);//reverse portrait
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            Settings.System.putInt(getContentResolver(), Settings.System.USER_ROTATION, Surface.ROTATION_0);//portrait
            Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);//turn on rotation according to accelerometer
        }
    }
    

    Do not forget adding permission to AndroidManifest.xml

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