Activity in portrait or reverse portrait only
Asked Answered
B

3

20

I want my activity to be available only in both portrait modes - portrait and reversePortrait. How can I achieve this? When I set android:screenOrientation="portrait" it will force activity to be only in normal portrait - vice versa with reversePortrait.

Please, don't tell me it's a bad approach to force/lock orientation. I know about it, but still client is requesting it. Thanks for understanding and for any ideas.

UPDATE: API Level 11 and higher

Baucom answered 7/10, 2011 at 11:17 Comment(3)
Not a bad approach IMO, some apps/games just won't work right in certain orientations.Phallicism
Thanks Ricky, exactly. I just saw people arguing about that on similar posts so I wanted to clarify that I really need only portrait.Baucom
many e-commerce application too locks orientationBusily
G
48

If you are on API level 9+, use android:screenOrientation="sensorPortrait".

Portrait orientation, but can be either normal or reverse portrait based on the device sensor. Added in API level 9.

Documentation

Glycol answered 7/10, 2011 at 11:21 Comment(6)
Thank you!!! I must be blind that I missed that value! And yes, I'm on API 11+ so this works perfectly.Baucom
I had to use the typo-ed version "sensorPortait" on api 15.Paprika
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?Broddy
i need to listen to orientation change also along with sensorPortait, any sugesstions?Chlo
NOTE I couldn't get sensorPortrait to work properly. It would never support upside down Portrait (i.e. reversePortrait). However, using fullSensor did work but it had the caveat of also allowing landscape. That ended up being fine for me but if you only want portrait and reversePortrait, you can intercept the orientation change in your Activity with onConfigurationChanged and newConfig.orientation and just ignore or override landscape changes.Babblement
But how to override without keeping super.onConfigurationChanged(newConfig) it will crash the app. So any suggestion how to handle if we don't want landscape mode?Eolian
P
1

For posterity's sake, I use this for backward compatibility...

public final class OrientationHelper {

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static void setRequestedOrientationSensorPortrait(Activity activity) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        } else {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    public static void setRequestedOrientationSensorLandscape(Activity activity) {
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.FROYO) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }
}

usage

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    OrientationHelper.setRequestedOrientationSensorPortrait(this);
    super.setContentView(R.layout.my_layout);
}
Pete answered 1/8, 2014 at 5:51 Comment(0)
H
0

In AndroidManifest.xml:

android:screenOrientation="portrait|reversePortrait"
android:configuration="keyboardHidden|orientation"

In your WhateverActivity.java:

protected void onConfigurationChanged(Configuration newConfig) {
    int currentOrientation = getResources().getConfiguration().orientation;
    if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ||
        newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
        super.onConfigurationChanged(newConfig);
    }
}

You can try this :)

Holna answered 7/10, 2011 at 11:32 Comment(3)
Thanks, this is a good answer but as I forgot to tell that I'm on API 11+ (Honeycomb) the alextc's answer is more suitable for me and works great. And I don't know why, but I got AAPT error when I try to use | in screenOrientation so I was not able to use two values but only one :(Baucom
@xjaphx, android:screenOrientation="portrait|reversePortrait". That doesn't work. The following error appears: "error: Error: String types not allowed (at 'screenOrientation' with value 'portrait|reversePortrait')".Allowedly
@PaulAnnekov Thas't because it's a simple enum, so you can't use a combination of values using "|"Isometrics

© 2022 - 2024 — McMap. All rights reserved.