1

I tried this, not work, I always got 0 degree

Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int screenOrientation = display.getRotation();

If use SensorManager, it is also not work, because it will not be triggered if I don't move the phone

mOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {

    @Override
    public void onOrientationChanged(int orientation) {
    }
};

Update

Please try the complete demo

https://github.com/sochap/getorientationquestdemo

Since the display is not changed, use display to determine that is impossible

I am going to get device orientation, not display orientation

OrientationEventListener will work, but not at onCreate

CL So
  • 3,455
  • 9
  • 44
  • 93
  • check the screen orientation write this code in oncreate "if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { //Landscape mode }" and if you want to check then you can check by portrait mode also – Hanuman Sep 07 '16 at 10:46
  • Possible duplicate of [How can I get the current screen orientation?](http://stackoverflow.com/questions/3663665/how-can-i-get-the-current-screen-orientation) – earthw0rmjim Sep 07 '16 at 10:47
  • You can check when the screen orientation changed by overriding onOrientationChaged in Activity – Hanuman Sep 07 '16 at 10:48
  • getResources().getConfiguration().orientation is always 1 – CL So Sep 07 '16 at 11:12
  • It is impossible to use display to determine, just try my demo – CL So Sep 08 '16 at 08:30

3 Answers3

1

This may work for you. try this.

 Configuration config = getResources().getConfiguration();

    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {

                     // set Values for landscape

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

                   // set Values for portrait
    }
Learning Always
  • 1,463
  • 2
  • 25
  • 46
0

Try

int currentOrientation = getResources().getConfiguration().orientation;

if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
     //do something
} else {
     //do something
} 
Mithun Sarker Shuvro
  • 3,729
  • 6
  • 32
  • 56
-1

Use the onConfigurationChanged method of Activity. See the following code:

@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();
  }
}

You also have to edit the appropriate element in your manifest file to include the android:configChanges Just see the code below:

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

NOTE: with Android 3.2 (API level 13) or higher , the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher , you must decalare android:configChanges="orientation|screenSize" for API level 13 or higher .

Hope this will help you..:)

Chirag Arora
  • 814
  • 8
  • 20
  • This method never be called, because I set `android:screenOrientation="portrait"` to activity, I also disabled rotation in Android – CL So Sep 07 '16 at 11:17
  • try this : int rotation = getWindowManager().getDefaultDisplay() .getRotation(); – Chirag Arora Sep 07 '16 at 11:21
  • int orientation; CharSequence text; switch (rotation) { case Surface.ROTATION_0: text = "SCREEN_ORIENTATION_PORTRAIT"; break; case Surface.ROTATION_90: text = "SCREEN_ORIENTATION_LANDSCAPE"; break; case Surface.ROTATION_180: text = "SCREEN_ORIENTATION_REVERSE_PORTRAIT"; break; case Surface.ROTATION_270: text = "SCREEN_ORIENTATION_REVERSE_LANDSCAPE"; break; default: text = "SCREEN_ORIENTATION_PORTRAIT"; break; } – Chirag Arora Sep 07 '16 at 11:23
  • try these and if you face any issue then let me know – Chirag Arora Sep 07 '16 at 11:23
  • @CL So Could you please provide your demo link – Chirag Arora Jun 06 '18 at 04:53