How to Check Device Natural (Default) Orientation on Android (I.E. Get Landscape for E.G., Motorola Charm or Flipout)

Get default device orientation (Landscape or Portrait) on Android/iOS

Actually found what I needed by myself !

Here's the link, for anyone interested : https://gist.github.com/flarb/3252857

(I didn't need the iOS check in the end, since our application is only supported on tablets anyway, plus I think the default orientation on all iOS devices is Portrait (might be wrong))

Cheers !

Getting the natural orientation of a device in CN1

I don't think there is a native API for getting the natural device orientation in any OS. Regardless, iOS only allows you to lock device orientation properly with a build hint so that wouldn't be very useful.

Different screen orientation on different devices when landscape mode

I have found the solution.

With this code I am getting the default display rotation:

int rotation = getWindowManager().getDefaultDisplay().getRotation();

in the body of my onOrientationChanged method I can make this:

            if (rotation == 0) {
if (orientation >= 90 && orientation <= 270) {

}
} else if (rotation == 1) {
if (orientation < 180 && orientation > 0 ) {

}
}

This will enter the if only if my device is not in the correct landscape position. If my device is on 90+ angle I can give the user a message.

How to detect the device orientation with SensorEventListener?

An OrientationEventListener will work, even with fixed orientation; see https://stackoverflow.com/a/8260007/1382108. It monitors the sensor according to the documentation.
Say you define the following constants:

private static final int THRESHOLD = 40;
public static final int PORTRAIT = 0;
public static final int LANDSCAPE = 270;
public static final int REVERSE_PORTRAIT = 180;
public static final int REVERSE_LANDSCAPE = 90;
private int lastRotatedTo = 0;

The numbers correspond to what the OrientationEventListener returns, so if you have a natural landscape device (tablet) you have to take that into account, see How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout).

 @Override
public void onOrientationChanged(int orientation) {
int newRotateTo = lastRotatedTo;
if(orientation >= 360 + PORTRAIT - THRESHOLD && orientation < 360 ||
orientation >= 0 && orientation <= PORTRAIT + THRESHOLD)
newRotateTo = 0;
else if(orientation >= LANDSCAPE - THRESHOLD && orientation <= LANDSCAPE + THRESHOLD)
newRotateTo = 90;
else if(orientation >= REVERSE_PORTRAIT - THRESHOLD && orientation <= REVERSE_PORTRAIT + THRESHOLD)
newRotateTo = 180;
else if(orientation >= REVERSE_LANDSCAPE - THRESHOLD && orientation <= REVERSE_LANDSCAPE + THRESHOLD)
newRotateTo = -90;
if(newRotateTo != lastRotatedTo) {
rotateButtons(lastRotatedTo, newRotateTo);
lastRotatedTo = newRotateTo;
}
}

The rotateButtons functions being something like:

public void rotateButtons(int from, int to) {

int buttons[] = {R.id.buttonA, R.id.buttonB};
for(int i = 0; i < buttons.length; i++) {
RotateAnimation rotateAnimation = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(200);
rotateAnimation.setFillAfter(true);
View v = findViewById(buttons[i]);
if(v != null) {
v.startAnimation(rotateAnimation);
}
}
}


Related Topics



Leave a reply



Submit