Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upside down rotation doesn't trigger configuration change

Tags:

android

When I turn my Android phone upside down, my Activity does not rotate to show the layout upside down but stays in landscape mode instead. I tried this with a very simple HelloWorld app. I added android:configChanges="orientation" to the manifest and overrode onConfigurationChange() in the Activity to set breakpoints there. Rotating the device upside down produces one config change to go from portrait (upside up) to landscape but no second change from landscape to portrait (upside down). Is this an Android issue or is there something I need to do?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hello.world"
    android:versionCode="1"
    android:versionName="1.0" >
   <uses-sdk android:minSdkVersion="10" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:configChanges="orientation"
            android:name=".HelloWorldActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Activity:

public class HelloWorldActivity
  extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }

  public void onConfigurationChanged(Configuration newConfig)
  {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
      Log.e("MDO", "orientation change: landscape");
    }
    else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
      Log.e("MDO", "orientation change: portrait");
    }
  }
}
like image 797
Mirza Dobric Avatar asked Jul 20 '12 19:07

Mirza Dobric


3 Answers

It's not an issue, it's how Android works. It views upside-down only in landscape mode and not in portrait (and in landscape from version 2.2 if I remember correctly). Configuration change occurs when it goes from portrait to landscape and vice versa. If you want to check if the phone was flipped upside down or in whatever direction you'll have to access accelerometer sensor. Here's a tutorial on how to use it and here you have SensorManager docs.

EDIT: as the author of the question found out himself, adding android:screenOrientation="fullSensor" to your manifest is enough, provided that you don't want to support anything older than Android 2.3 (API level 9).

like image 194
Michał Klimczak Avatar answered Nov 15 '22 07:11

Michał Klimczak


Include screenSize in your configChanges in your mafifest:

android:configChanges="orientation|screenSize"

From http://developer.android.com/guide/topics/resources/runtime-changes.html:

Beginning with Android 3.2 (API level 13), 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 (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

like image 31
Iñigo Avatar answered Nov 15 '22 09:11

Iñigo


Setting android:screenOrientation="fullSensor" accomplishes what I'm trying to do.

like image 25
Mirza Dobric Avatar answered Nov 15 '22 09:11

Mirza Dobric