Find out if the orientation of the device is locked (detection if auto-rotation is on / off)

How can I find out if the screen orientation of the device is locked? I use OrientationEventListener to trigger some actions inside my application that I would like to disable if the user has locked their screen.

I know that usually I can navigate this way, but how do I know this is a locked orientation:

    int orientation = getResources().getConfiguration().orientation;

    if (orientation == Configuration.ORIENTATION_PORTRAIT) {
        // It portrait
    } else {
        // It landscape
    }
+4
source share
2 answers

Use this:

if (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1){
        Toast.makeText(getApplicationContext(), "Auto Rotate is ON", Toast.LENGTH_SHORT).show();

    }
    else{
        Toast.makeText(getApplicationContext(), "Auto Rotate is OFF", Toast.LENGTH_SHORT).show();
    }
+16
source

You can check the orientation at runtime, for example:

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

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();        
}
}

Hope this helps.

-1
source

Source: https://habr.com/ru/post/1623115/


All Articles