Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Android orientation: landscape-Left v. landscape-Right

Tags:

android

How can I detect which one of 4 sides of the phone is up.

I can detect portrait/landscape mode, but how do I tell landscape-turned-on-left-side from landscape-turned-on-right-side?

Basically I want to make a nice transition animation when user turns phone. You know, like in iPhone's Safari: a swift 400ms rotation from the previous layout to the new.

like image 968
Oleg Mihailik Avatar asked Jan 18 '11 18:01

Oleg Mihailik


People also ask

How do I know what orientation my Android phone is?

getDefaultDisplay(); int rotation = display. getRotation(); Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.

How can we detect the orientation of the screen?

Detecting screen orientation is easy - and in the future we'll be able to use screen. orientation to reliably do this. For now, it's best to stick with CSS media queries and window. matchMedia .

How do I know if my screen is portrait or landscape?

Things You Should KnowSwipe down from the top of your screen to enable "Auto-Rotate," then turn your phone to the side to switch to landscape mode. Turn your phone upright to switch back to portrait mode.


2 Answers

Just came across :) 2.2+ put xml code to ur res/values(false) and res/values-xlarge(true)

<resources>
<bool name="isTablet">false</bool>

private void getScreenRotationOnPhone() {

    final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    switch (display.getRotation()) {
        case Surface.ROTATION_0:
            System.out.println("SCREEN_ORIENTATION_PORTRAIT");
            break;

        case Surface.ROTATION_90:
            System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
            break;

        case Surface.ROTATION_180:
            System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
            break;

        case Surface.ROTATION_270:
            System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
            break;
    }
}

private void getScreenRotationOnTablet() {

    final Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    switch (display.getRotation()) {
        case Surface.ROTATION_0:
            System.out.println("SCREEN_ORIENTATION_LANDSCAPE");
            break;

        case Surface.ROTATION_90:
            System.out.println("SCREEN_ORIENTATION_REVERSE_PORTRAIT");
            break;

        case Surface.ROTATION_180:
            System.out.println("SCREEN_ORIENTATION_REVERSE_LANDSCAPE");
            break;

        case Surface.ROTATION_270:
            System.out.println("SCREEN_ORIENTATION_PORTRAIT");
            break;
    }
}

private boolean isTabletDevice(){
    boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
    if (tabletSize) {
        return true;
    } else {
        return false;
    }
}
like image 199
Piotr Ślesarew Avatar answered Jan 29 '23 18:01

Piotr Ślesarew


Use an OrientationEventListener:

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) 
{ 
 @Override
 public void onOrientationChanged(int orientation) 
 {
  mDeviceOrientation = orientation;
 }
};

if(mOrientationEventListener.canDetectOrientation())
{
 mOrientationEventListener.enable();
}

mDeviceOrientation should then be an integer telling you the angle your device is rotated to, if you do some clever rounding you should be able to see which of the four orientations it is in:

// Divide by 90 into an int to round, then multiply out to one of 5 positions, either 0,90,180,270,360. 
int orientation = 90*Math.round(mDeviceOrientation / 90); 

// Convert 360 to 0
if(orientation == 360)
{
    orientation = 0;
}

Enjoy!

like image 21
jsonfry Avatar answered Jan 29 '23 18:01

jsonfry