1

In my app I am using the front camera to create preview frame, before opening the camera I want to know if Orientation of device in Landscape Mode is clockwise (Right side) or anticlockwise (Left Side).

I am getting the Orientation by

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE

But let me know how to check it is clockwise or anticlockwise?

NullPointer
  • 7,094
  • 5
  • 27
  • 41

1 Answers1

1

Check out this snippet : Activity.class

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
    if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_0){
        // clockwise
    }else if(getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_180){
        // anti-clockwise
    }
} else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    //portrait
}

Not just to check at the start, but you should also check that if it's getting changed in runtime too.

AndroidManifest.xml

<activity
    android:name=".MainActivity"
    android:configChanges="orientation"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">

MainActivity.java

@Override
public void onConfigurationChanged(Configuration newConfig) {
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        //landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        //portrait
    }
}
exploitr
  • 843
  • 1
  • 14
  • 27