237

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?

Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

9 Answers9

531
Activity.getResources().getConfiguration().orientation
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 16
    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) – HRJ May 04 '11 at 09:56
  • 21
    @HRJ you can use getWindowManager().getDefaultDisplay().getRotation() – MKJParekh Dec 09 '11 at 12:10
  • 29
    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. – Zoltán Apr 30 '12 at 08:40
93
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

Peter Samokhin
  • 864
  • 1
  • 9
  • 19
Daniel
  • 1,599
  • 1
  • 16
  • 19
  • 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, though – Eric Apr 28 '23 at 23:16
35
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;
}
Linh
  • 57,942
  • 23
  • 262
  • 279
AndroidBeginner
  • 375
  • 3
  • 8
  • 1
    `final int angle = (rotation == Surface.ROTATION_90) ? 90 : (rotation == Surface.ROTATION_180) ? 180 : (rotation == Surface.ROTATION_270) ? 270 : 0;` – Andrii Syrokomskyi Jun 28 '18 at 00:04
  • Note that this is relative to the device's "natural" orientation. "0" on a tablet is landscape, "0" on a phone is portrait. – ToolmakerSteve Oct 23 '22 at 01:26
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
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Sakthimuthiah
  • 2,606
  • 6
  • 26
  • 41
  • 1
    Please note that getOrient.getWidth() and getOrient.getHeight() are deprecated now. – valerybodak Mar 27 '15 at 07:31
  • Should be marked as the correct answer as is way more detailed – Zapnologica May 19 '15 at 09:33
  • 1
    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. – Mahendra Chhimwal Sep 24 '15 at 04:49
  • like Point outSize=new point(); getOrient.getSize(outSize);if(outSize.x==outSize.y)return SQUARE; – Mahendra Chhimwal Sep 24 '15 at 04:51
  • 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. – Dave Feb 10 '16 at 18:27
15
getActivity().getResources().getConfiguration().orientation

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

Ankur
  • 5,086
  • 19
  • 37
  • 62
Rudolf Coutinho
  • 215
  • 3
  • 11
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()

a.ch.
  • 8,285
  • 5
  • 40
  • 53
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

Z3nk
  • 365
  • 4
  • 17
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!!

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.

Samarth S
  • 313
  • 4
  • 5