4

For my Android app, the value returned for activity.getResources().getConfiguration().orientation is always 1 (ORIENTATION_PORTRAIT), even when the device is in landscape mode. This is happening specifically on Samsung galaxy devices. This is causing my fragment to use the portrait layout instead of the landscape layout present in the project. Is there a different way to check the orientation on Samsung devices? Is anyone else facing the same issue?

    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

    // Get device orientation
    Configuration config = activity.getResources().getConfiguration();
    mOrientation = config.orientation;
Lalit Agarwal
  • 173
  • 4
  • 14
  • 1
    Samsung... But you could try to do a workaround by using resources, as described here https://stackoverflow.com/a/14929272/6386583 – G. Kalender Feb 13 '18 at 23:04
  • Possible duplicate of [Check orientation on Android phone](https://stackoverflow.com/questions/2795833/check-orientation-on-android-phone) – G. Kalender Feb 13 '18 at 23:05

1 Answers1

1

You are not the first to complain about this flaw on some Samsung phones: control orientation by sensor on Samsung devices. If you don't care about the case of SCREEN_ORIENTATION_REVERSE_PORTRAIT, maybe, for you SCREEN_ORIENTATION_SENSOR will work better than SCREEN_ORIENTATION_FULL_SENSOR.

I am not surprised that this manufacturer did something that doesn't work for you as expected. I believe that you can still use the OrientationListener to derive the true device orientation from accelerometer, like in How to detect my screen orientation in portrait locked screen in android? .

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I tried to use SCREEN_ORIENTATION_SENSOR but that didn't work either. When my device was in landscape mode, getResources().getConfiguration().orientation was still returning 1 (ORIENTATION_PORTRAIT). – Lalit Agarwal Feb 14 '18 at 16:25
  • So, the OrientationListener is your way – Alex Cohn Feb 15 '18 at 07:35
  • Yes, definitely I can try that solution but ideally what I want is that when I inflate a layout, it should automatically detect which layout folder to use (portrait or landscape). Setting the correct sensor would be really useful in that case. That is why I was trying to set the right sensor but not able to do so till now. – Lalit Agarwal Feb 15 '18 at 15:52
  • 1
    My preference has always been to keep camera preview in one predefined orientation, probably landscape, like all preinstalled camera apps. The motivation is that I want to keep the viewfinder smooth even when the device is rotated. I still must keep track of orientation, at least to set correct EXIF rotation flag. – Alex Cohn Feb 15 '18 at 19:14