2

I have an oddball android device which has no gravity sensors, and on that device, my "set orientaion" function does nothing. On all other devices, including some without sensors, the following works fine.

        Activity act = AndroidNativeUtil.getActivity();
    int newo = portrait 
        ?   (reverse
                ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
                : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
        :   (reverse
                ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    act.setRequestedOrientation(
            newo

is there some additional API that can lock the screen orientation? Some other apps successfully change the screen orientation.

ddyer
  • 1,792
  • 19
  • 26
  • Is portrait vs landscape not working? Or just portrait/reverse portrait? – Gabe Sechan Dec 20 '20 at 05:30
  • 1
    in my app, no rotation occurs. Using a 3'rd party rotator app, reverse landscape doesn't work, but the other 3 orientations work. – ddyer Dec 20 '20 at 19:08
  • Presence of sensors has nothing to do with orientation support. The default android screen has 3 axes mapped to the display panel / device body. `setRequestedOrientation()` should work, unless the device's manufacturer has provided a really poor implementation of this API. – S.D. Jan 04 '21 at 11:16
  • Have you checked if the orientation on that device is locked? May that be a device, that never changes orientation (if there's anything like that)? Check this out: https://stackoverflow.com/questions/34636464/find-out-if-device-orientation-is-locked-detect-if-auto-rotate-is-enabled-disab – Janos Vinceller Jan 06 '21 at 14:56
  • You could also check the answer from "Paul", he describes different orientation constants for different API versions: https://stackoverflow.com/questions/18268218/change-screen-orientation-programmatically-using-a-button – Janos Vinceller Jan 06 '21 at 15:03

2 Answers2

3

Check orientation

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

How to lock orientation?

public class OrientationUtils {
private OrientationUtils() {}

/** Locks the device window in landscape mode. */
public static void lockOrientationLandscape(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

/** Locks the device window in portrait mode. */
public static void lockOrientationPortrait(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

/** Allows user to freely use portrait or landscape mode. */
public static void unlockOrientation(Activity activity) {
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}

}
0

How about android:screenOrientation="locked". This will lock the orientation to its current rotation (From API 18)

dinhlam
  • 708
  • 3
  • 14