Android: get current device orientation

I have an activity orientation in a manifest set as a portrait. Because I do not want my screen to rotate.

But pressing the button, I want to get the current orientation of the device (portrait or landscape) in Android? I do not need continuous updates from sensors; just the current orientation of the device when i press the button is enough.

PS: Device orientation is the way I hold my device. Do not mix it with the screen orientation.

Is that what I have done so far?

int PORT=1,LAND=2; WindowManager windowService = (WindowManager)getSystemService(Context.WINDOW_SERVICE); int rotation = windowService.getDefaultDisplay().getRotation(); if (Surface.ROTATION_0 == rotation) { rotation = PORT; } else if(Surface.ROTATION_180 == rotation) { rotation = PORT; } else if(Surface.ROTATION_90 == rotation) { rotation = LAND; } else if(Surface.ROTATION_270 == rotation) { rotation = LAND; } return rotation; 

Even using the following method does not help

 getResources().getConfiguration().orientation 

It always returns 1, since the default portrait is set for my work.

Sample / sample code will be highly appreciated.

Thanks.

+7
android screen-orientation android-orientation device-orientation
source share
1 answer

Use it :)

 getResources().getConfiguration().orientation 
+4
source share

All Articles