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");
}
}
}
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
SensorManagerdocs.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).