How can I get the current screen orientation?
Asked Answered
H

9

241

I just want to set some flags when my orientation is in landscape so that when the activity is recreated in onCreate() i can toggle between what to load in portrait vs. landscape. I already have a layout-land xml that is handling my layout.

public void onConfigurationChanged(Configuration _newConfig) {

        if (_newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            this.loadURLData = false;
        }

        if (_newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            this.loadURLData = true;
        }

        super.onConfigurationChanged(_newConfig);
    }

Over-riding onConfigurationChanged will prevent my layout-land xml from loading in landscape orientation.

I just want to get the current orientation of my device in onCreate(). How can I get this?

Heatstroke answered 8/9, 2010 at 0:15 Comment(0)
M
535
Activity.getResources().getConfiguration().orientation
Mink answered 8/9, 2010 at 0:22 Comment(3)
This only provides two values ORIENTATION_PORTRAIT and ORIENTATION_LANDSCAPE. Is there a way to get all the four values from ActivityInfo? (That is LANDSCAPE_REVERSE and PORTRAIT_REVERSE as well)Hawger
@Hawger you can use getWindowManager().getDefaultDisplay().getRotation()Asthenopia
getWindowManager().getDefaultDisplay().getRotation() returns the rotation with reference to the display's "natural" orientation, i.e. for a tablet it would return Surface.ROTATION_0 in landscape mode, whereas for a mobile phone it would return the same value for portrait.Eugenioeugenius
S
96
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
    // code for portrait mode
} else {
    // code for landscape mode
}

When the superclass of this is Context

Systematic answered 7/3, 2015 at 1:3 Comment(1)
from my testing, this seems to work when this is an Application, too. I wonder if it is guaranteed to work for all Android versions, thoughBonina
N
36
int rotation =  getWindowManager().getDefaultDisplay().getRotation();

this will gives all orientation like normal and reverse

and handle it like

int angle = 0;
switch (rotation) {
    case Surface.ROTATION_90:
        angle = -90;
        break;
    case Surface.ROTATION_180:
        angle = 180;
        break;
    case Surface.ROTATION_270:
        angle = 90;
        break;
    default:
        angle = 0;
        break;
}
Nuthatch answered 29/5, 2014 at 9:42 Comment(2)
final int angle = (rotation == Surface.ROTATION_90) ? 90 : (rotation == Surface.ROTATION_180) ? 180 : (rotation == Surface.ROTATION_270) ? 270 : 0;Epididymis
Note that this is relative to the device's "natural" orientation. "0" on a tablet is landscape, "0" on a phone is portrait.Agave
L
30

In some devices void onConfigurationChanged() may crash. User will use this code to get current screen orientation.

public int getScreenOrientation()
{
    Display getOrient = getActivity().getWindowManager().getDefaultDisplay();
    int orientation = Configuration.ORIENTATION_UNDEFINED;
    if(getOrient.getWidth()==getOrient.getHeight()){
        orientation = Configuration.ORIENTATION_SQUARE;
    } else{ 
        if(getOrient.getWidth() < getOrient.getHeight()){
            orientation = Configuration.ORIENTATION_PORTRAIT;
        }else { 
             orientation = Configuration.ORIENTATION_LANDSCAPE;
        }
    }
    return orientation;
}

And use

if (orientation==1)        // 1 for Configuration.ORIENTATION_PORTRAIT
{                          // 2 for Configuration.ORIENTATION_LANDSCAPE
   //your code             // 0 for Configuration.ORIENTATION_SQUARE
}
Limbert answered 30/4, 2013 at 8:32 Comment(5)
Please note that getOrient.getWidth() and getOrient.getHeight() are deprecated now.Mistook
Should be marked as the correct answer as is way more detailedDeangelo
Should be marked as correct answer and works best in all conditions. But please note as anivaler said getActivity().getWindowManager().getDefaultDisplay().getWidth() and getHeight() is deprecated now. Please use getSize(Point outsize). It can be used as passing new point object and that will get its x and y members.Afterguard
like Point outSize=new point(); getOrient.getSize(outSize);if(outSize.x==outSize.y)return SQUARE;Afterguard
Thought I would point out, that Configuration.ORIENTATION_SQUARE is deprecated and while it was may have been defined as 0 in 2013, it is defined as 3, while Configuration.ORIENTIATION_UNDEFINED is defined as 0. Cheers.Moonscape
G
15
getActivity().getResources().getConfiguration().orientation

this command returns int value 1 for Portrait and 2 for Landscape

Glib answered 5/6, 2013 at 6:13 Comment(0)
V
4

In case anyone would like to obtain meaningful orientation description (like that passed to onConfigurationChanged(..) with those reverseLandscape, sensorLandscape and so on), simply use getRequestedOrientation()

Vaden answered 20/2, 2014 at 13:30 Comment(0)
I
2

I just want to propose another alternative that will concern some of you :

android:configChanges="orientation|keyboardHidden" 

implies that we explicitly declare the layout to be injected.

In case we want to keep the automatic injection thanks to the layout-land and layout folders. All you have to do is add it to the onCreate:

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        getSupportActionBar().hide();

    } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        getSupportActionBar().show();
    }

Here, we display or not the actionbar depending on the orientation of the phone

Inflight answered 7/2, 2019 at 9:59 Comment(0)
Y
1

In your activity class use the method below :

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

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            setlogo();// Your Method
            Log.d("Daiya", "ORIENTATION_LANDSCAPE");

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {

            setlogoForLandScape();// Your Method
            Log.d("Daiya", "ORIENTATION_PORTRAIT");
        }
    }

Then to declare that your activity handles a configuration change, edit the appropriate element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute (the most commonly used values are "orientation" to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes). You can declare multiple configuration values in the attribute by separating them with a pipe | character.

<activity android:name=".MyActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">

That's all!!

Yellowbird answered 16/6, 2017 at 7:13 Comment(0)
C
0

if you want to override that onConfigurationChanged method and still want it to do the basic stuff then consider using super.onConfigyrationChanged() as the first statement in the overriden method.

Crap answered 14/7, 2016 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.