prashant prashant - 21 days ago 8
Android Question

How to keep the screen in landscape mode in Android

I have an activity which should always be displayed in Landscape mode. So i added

android:screenOrientation="landscape"
.

But the problem is when I rotate the device by 180 degrees, the display is inverted. is there a way to handle this issue so that the screen elements are always shown correctly.?

Answer

So just for everyone information, this is what i did.

  1. In Android manifest added android:screenOrientation="landscape".
  2. In on resume method add these lines of code
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getRotation();

if(orientation==Surface.ROTATION_180)
{
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}

This way my screen is always positioned correctly even if user holds the device upside down.