2

To get orientation, I have tried the below codes. But I don't get the desired output.

int rotation = getWindowManager().getDefaultDisplay().getRotation();

and

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

I have tried this both of code and rotate the device every angle but always get rotation value 0.

Then I have tried this below code

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

But now I am always getting rotation value 1. How I will get different value of rotation for Natural orientation, Landscape left, Upside down, Landscape right?

Benjamin Scholtz
  • 823
  • 6
  • 15
spectre_d2
  • 193
  • 15
  • 1
    Look this: https://stackoverflow.com/questions/5112118/how-to-detect-orientation-of-android-device regards, – Jsperk Aug 14 '18 at 14:34

2 Answers2

1

You can use getRequestedOrientation() and it will return one of these values.

Hope this helps !

Neïl Rahmouni
  • 326
  • 2
  • 10
0
Activity.getResources().getConfiguration().orientation
Kintan Patel
  • 1,230
  • 8
  • 15
  • I have tried this but get a compilation error like "Non-static method 'getResources() can not be referenced from a static context'" . Then I have tried this.getResources().getConfiguration().orientation and this.getBaseContext().getResources().getConfiguration().orientation, it doesn't solve my problem. – spectre_d2 Aug 14 '18 at 14:20