I tried getOrientation()
to get the orientation value but it always returns 0!

- 39,853
- 6
- 84
- 117

- 261
- 1
- 5
- 13
-
7Try rotating the device first. – noelicus Aug 19 '12 at 22:14
-
Rolled back 3rd party title edit as the question (and even more so it's answers) concern orientation as a general problem rather than that one method in particular. – Chris Stratton May 24 '13 at 20:01
8 Answers
getOrientation() is deprecated but this is not neccesarily the root of your problem. It is true that you should use getRotation() instead of getOrientation() but you can only use it if you are targeting Android 2.2 (API Level 8) or higher. Some people and even Googlers sometimes seem to forget that.
As an example, on my HTC desire running Android 2.2. getOrientation() and getRotation() both report the same values:
- 0 (default, portrait),
- 1 (device 90 degree counterclockwise),
- 3 (device 90 degree clockwise)
It does not report when you put it "on its head" (rotate 180, that would be the value 2). This result is possibly device-specific.
First of all you should make clear if you use an emulator or a device. Do you know how to rotate the emulator? Then I would recommend to create a small test program with a onCreate() Method like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display mDisplay = mWindowManager.getDefaultDisplay();
Log.d("ORIENTATION_TEST", "getOrientation(): " + mDisplay.getOrientation());
}
Check if the screen of your your device has been locked in the device settings Settings > Display > Auto-Rotate Screen. If that checkbox is unchecked, Android will not report orientation changes to your Activity. To be clear: it will not restart the activity. In my case I get only 0, like you described.
You can check this from your program if you add these lines to onCreate()
int sysAutoRotate = 0;
try {
sysAutoRotate = Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION);
} catch (SettingNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ORIENTATION_TEST", "Auto-rotate Screen from Device Settings:" + sysAutoRotate);
It will return 0 if Auto-Rotate is off and 1 if Auto-Rotate is on. Another try. In your original program you might have set in manifest
android:screenOrientation="portrait"
Same effect, but this time for your activity only. If you made the small test program this possibilty would have been eliminated (that's why I recommend it).
Remark: Yes the whole orientation / rotation topic is an "interesting" topic indeed. Try things out, use Log.d(), experiment, learn.

- 6,487
- 3
- 39
- 47

- 985
- 1
- 9
- 16
If you want to know if the content currently displayed is in landscape mode or portrait (possibly completely independent of the phone's physical rotation) you can use:
getResources().getConfiguration().orientation
public int orientation
Since: API Level 1
Overall orientation of the screen. May be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, or ORIENTATION_SQUARE.

- 27,671
- 19
- 68
- 123

- 10,037
- 1
- 39
- 33
-
Good solution when you need to support older OS versions. Seems to be more reliable than `getOrientation()`. – dvs Nov 22 '11 at 00:54
-
-
it seems like getOrientation() always returned ORIENTATION_UNDEFINED if i hadn't rotated the screen since loading the activity. this way avoids that problem. – Keith Oct 15 '12 at 22:53
getOrientation()
is deprecated. Instead, try getRotation()
.

- 5,325
- 2
- 27
- 22
-
Any idea why getOrientation is deprecated? As far as I can tell, getRotation does the exact same thing. – Edward Falk Feb 26 '11 at 16:47
-
1If you must support API 7, you can't use `getRotation`, it's API 8 onwards. – mxcl Mar 20 '12 at 12:38
To avoid use of Deprecated methods use the Android Configuration class found here. It works since API lvl 1 and still works on the latest android devices. (Not deprecated).
As and example consider the following code snippet:
Configuration config = getResources().getConfiguration();
if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.portraitStart);
}
else
{
setContentView(R.layout.landscapeStart);
}
Best of luck- hope this answer helps whoever runs into it.

- 1
- 1

- 2,056
- 23
- 32
-
1Why would you manually switch via code? Layout configurations are meant for that. /res/layout and /res/layout-land. – afollestad Jul 21 '15 at 01:31
You should use:
getResources().getConfiguration().orientation
It can be one of ORIENTATION_LANDSCAPE
, ORIENTATION_PORTRAIT
.
http://developer.android.com/reference/android/content/res/Configuration.html#orientation

- 7,041
- 11
- 44
- 67

- 484
- 2
- 13
-
How do you get more detailed orientations like landscape left/right? – Mark13426 Oct 27 '16 at 02:30
I think you need to create two different layouts and inflate different layouts on different orientation. I am giving you a sample code which is working fine for me. "context" you can pass from your activity in case of custom adapter. If you are using custom adapter then you can try this code:
@Override
public View getView(final int position,
View convertView,
ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
int i = context.getResources().getConfiguration().orientation;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (i == Configuration.ORIENTATION_PORTRAIT) {
rowView = inflater.inflate(R.layout.protrait_layout, null, false);
} else {
rowView = inflater.inflate(R.layout.landscape_layout, null, false);
}
holder = new ViewHolder();
holder.button = (Button) rowView.findViewById(R.id.button1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
return rowView;
}
other wise you can directly set two different layouts in your code
int i = context.getResources().getConfiguration().orientation;
if (i == Configuration.ORIENTATION_PORTRAIT) {
ArrayAdapter<String> adt = new ArrayAdapter<String>(getApplicationContext(), R.layout.protrait_layout, name);
} else {
ArrayAdapter<String> adt = new ArrayAdapter<String>(getApplicationContext(), R.layout.landscape_layout, name);
}

- 3,926
- 2
- 17
- 29
/* First, get the Display from the WindowManager */
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
/* Now we can retrieve all display-related infos */
int width = display.getWidth();
int height = display.getHeight();
int orientation = display.getOrientation();

- 5,514
- 5
- 35
- 41