1

I tried this, not work, I always got 0 degree

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenOrientation = display.getRotation();

If use SensorManager, it is also not work, because it will not be triggered if I don't move the phone

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

    @Override
    public void onOrientationChanged(int orientation) {
    }
};

Update

Please try the complete demo

https://github.com/sochap/getorientationquestdemo

Since the display is not changed, use display to determine that is impossible

I am going to get device orientation, not display orientation

OrientationEventListener will work, but not at onCreate

CL So
  • 3,647
  • 10
  • 51
  • 95
  • check the screen orientation write this code in oncreate "if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { //Landscape mode }" and if you want to check then you can check by portrait mode also – Hanuman Sep 07 '16 at 10:46
  • Possible duplicate of [How can I get the current screen orientation?](http://stackoverflow.com/questions/3663665/how-can-i-get-the-current-screen-orientation) – earthw0rmjim Sep 07 '16 at 10:47
  • You can check when the screen orientation changed by overriding onOrientationChaged in Activity – Hanuman Sep 07 '16 at 10:48
  • getResources().getConfiguration().orientation is always 1 – CL So Sep 07 '16 at 11:12
  • It is impossible to use display to determine, just try my demo – CL So Sep 08 '16 at 08:30

2 Answers2

1

This may work for you. try this.

 Configuration config = getResources().getConfiguration();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {

                     // set Values for landscape

 } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {

                   // set Values for portrait
    }
Learning Always
  • 1,563
  • 4
  • 29
  • 49
0

Try

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

if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
     //do something
} else {
     //do something
} 
Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64