How can I rotate display only in landscape mode in android?
Asked Answered
T

4

24

I want that my View rotates only in landscape mode, clockwise and counterclockwise.

I read about the only counterclockwise for android < 2.2 and that's not a problem, my App will be +2.2 for now.

I modify my manifest to catch Configuration Changes

android:configChanges="keyboardHidden|orientation"

I override my activity to catch Configuration Changes

@Override
public void onConfigurationChanged(Configuration newConfig) {

and I know how to catch orientation

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

but... I don't know how to trigger the appropiate landscape orientation, I am doing this:

if (rot == Surface.ROTATION_90 || rot == Surface.ROTATION_270){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

but always rotate to counterclocwise :-(

How can I set left and right landscape orientation?

EDIT

If I set orientation in manifest:

android:screenOrientation="landscape"

The activity's layout remains always in "left landscape", and I want change between left and right landscape :S

Timbal answered 21/3, 2011 at 11:55 Comment(1)
I have the same problem with my application. I'm using API8 and I cannot find a way to enable only landscape mode (both CW & CCW)Refute
Q
61

If you're building your app for Android 2.3 and newer you should set the manifest attribute as

android:screenOrientation="sensorLandscape"

and your app will rotate to either (left or right) landscape position.

If you're building your app for Android 2.2 and older but want to run it on Android 2.3 and newer as a "sensorLandscape" configuration, you could try something like this

public static final int ANDROID_BUILD_GINGERBREAD = 9;
public static final int SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 6;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= ANDROID_BUILD_GINGERBREAD) {
        setRequestedOrientation(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
...

This was the best way to handle the landscape orientation changes in my case. I was not able to find any better way to rotate the screen to left or right landscape orientations for Android 2.2 and older. I tried reading sensor orientations and setting the landscape position based on that, but it seems to me that as soon as you call "setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);" sensor turns off (for your app/activity) and you cannot read orientation through sensor.

BTW you don't really need to override the "onConfigurationChanged(Configuration newConfig)" for any of this to work properly.

Quatrain answered 13/1, 2012 at 15:26 Comment(4)
No need to define your own gingerbread and sensor landscape fint int variables, use Build.VERSION_CODES.GINGERBREAD and ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPEOrndorff
You have to define your own variables because Build.VERSION_CODES.GINGERBREAD and ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE are not (yet) defined on Android 2.2 and older platforms (because they didn't exist back then obviously). This fix is ment to work on Android 2.2 and older Android OSes.Quatrain
The older Android libraries don't support Build.VERSION.SDK_INT so I use Integer.parseInt(android.os.Build.VERSION.SDK). Also the android:screenOrientation="landscape" has to be left in the manifest (not mentioned in the answer here).Raby
I think there are 3 mode we could use for landscape mode: lanscape, reverseLanscape, sensorLandscaped This really awsome!Estevan
Y
10

In your manifest activity declaration, instead of

android:screenOrientation = "landscape"

use

android:screenOrientation = "sensorLandscape"

Yacht answered 5/3, 2015 at 12:56 Comment(0)
B
0

Have you tried adding this to your manifest inside the activity tag to see if it handles it automatically?

android:screenOrientation = "landscape"
Bought answered 21/3, 2011 at 12:19 Comment(3)
When I do that the activity starts on landscape mode, but always the left landscape with trackball on the right.Timbal
Is this on one or multiple devices, and which?Bought
Nexus One 2.3.3, the project is configured as API 8Timbal
W
0

AndroidManifest.xml -> Application -> selected your activity -> Screen orientation = landscape.

Winser answered 21/3, 2011 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.