25

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.?

Aman Gupta - ΔMΔN
  • 2,801
  • 2
  • 18
  • 39
prashant
  • 3,540
  • 7
  • 39
  • 50

6 Answers6

61

Actually what you really want is to specify:

android:screenOrientation="sensorLandscape"

in your AndroidManifest.xml. This will listen to the sensor data while snapping between landscape and reverseLandscape.

CaseyB
  • 24,495
  • 12
  • 73
  • 109
  • +1 Thanx CaseyB, it just take 1 minute to implement both the orientation. Once again thanx for saving my valuable time. – Paresh Mayani Nov 15 '11 at 06:51
  • 1
    FYI, it works only for Tablet, i have tested the same but it always display app in portrait mode by default i.e. its not working in phone – Paresh Mayani Nov 15 '11 at 08:59
  • 2
    sensorLandscape was added into Froyo (2.2) so it will work on any devices that have that or later. – CaseyB Nov 15 '11 at 18:10
  • 3
    This rotates the view alright but doesn't fire any event. I need to capture the event when orientation change from normal to reverse view. How to do that? – StarDust Aug 24 '12 at 08:33
  • Just wondering, why do you need that? Isn't landscape just landscape no matter what? – CaseyB Aug 24 '12 at 15:46
10

Others have mentioned sensorLandscape...to do this programmatically in your activity (or a base activity), you can set your orientation to that:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

I'm doing it in onResume(). This will respect landscape and reverse landscape when you flip the device around 180 degrees in the middle of the activity, without having to use onConfigurationChanged().

This was helpful to me since for tablets I need landscape/landscape reverse only, and for phones I need portrait/portrait reverse only, and don't want to do two separate AndroidManifest files. ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT also exists.

annie
  • 1,347
  • 1
  • 13
  • 25
8

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.

Paresh Mayani
  • 125,853
  • 70
  • 238
  • 294
prashant
  • 3,540
  • 7
  • 39
  • 50
2

In the Android Manifest write this:

   android:configChanges="orientation"
              android:screenOrientation="landscape"

like in the example below.

 <activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:configChanges="orientation"
              android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
nyalex5000
  • 93
  • 1
  • 1
  • 5
1

The good method is to use

setRequestedOrientation(6);

6 is the value for sensorLandscape but it seem's that there is no defined constant for it.

webshaker
  • 471
  • 6
  • 17
0

Hmmm. This depends a bit on the framework version you're using as well. Try this ast a start:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

If that works great. If it dosen't you have to tell us a little more about the layout of your app.

Taranasus
  • 515
  • 5
  • 12
  • Yes. i did that. here is what is put in my onResume method Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int orientation = display.getRotation(); if((orientation==Surface.ROTATION_90) || (orientation==Surface.ROTATION_270)){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }else if(orientation==Surface.ROTATION_180){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); – prashant Mar 24 '11 at 19:14