android screen orientation-ThrowExceptions

Exception or error:

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

How to solve:

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.

###

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

Developer Documentation

public int orientation
Since: API Level 1

Overall orientation of the screen. May be one of ORIENTATION_LANDSCAPE, ORIENTATION_PORTRAIT, or ORIENTATION_SQUARE.

###

getOrientation() is deprecated. Instead, try getRotation().

###

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.

###

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

###

/* 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();

###

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

###

I had the same problem on NexusOne SDK 2.3.

This solved it: Check orientation on Android phone.

Leave a Reply

Your email address will not be published. Required fields are marked *