7

I have a simple hello world Android testing project. In my AndroidManifest.xml, I already set

android:screenOrientation="portrait" 
android:configChanges="keyboardHidden|keyboard|orientation">

but when I debug my code, the variable isLandscape is True when it should supposed to be False

boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

I know that I can also set the activity orientation by code, but I need to set it in xml for some reasons. How can I do it?

Edit: my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidgames.mreater"
android:versionCode="1"
android:versionName="1.0" 
android:installLocation="preferExternal">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
    android:icon="@drawable/ic_launcher"
    android:allowBackup="true"
    android:label="Mr. Eater" >
    <activity
        android:name="com.androidgames.mreater.MrEaterGame"
        android:label="Mr. Eater" 
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

my actual onCreate activity method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    int frameBufferWidth = isLandscape ? 480 : 320;
    int frameBufferHeight = isLandscape ? 320 : 480;
   Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

    float scaleX = (float) frameBufferWidth
            / getWindowManager().getDefaultDisplay().getWidth();
    float scaleY = (float) frameBufferHeight
            / getWindowManager().getDefaultDisplay().getHeight();

    renderView = new AndroidFastRenderView(this, frameBuffer);
    graphics = new AndroidGraphics(getAssets(), frameBuffer);
    fileIO = new AndroidFileIO(getAssets());
    audio = new AndroidAudio(this);
    input = new AndroidInput(this, renderView, scaleX, scaleY);
    screen = this.getStartScreen();
   setContentView(renderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame");
}

Now it's getting more weird, the isLandscape variable is True, but sometimes it is False. It's somehow like a bug.

Cuồn Yết
  • 135
  • 1
  • 1
  • 13

2 Answers2

12

Make sure you put it in the <activity> tag, not the <application> tag.

It only works in the <activity> tag, but will not complain if you put it in the <application> tag.

http://developer.android.com/guide/topics/manifest/application-element.html

vs

http://developer.android.com/guide/topics/manifest/activity-element.html

You will need to put it for each Activity you define in your manifest.xml

Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • thanks Ken Wolf but I put it in activity tag, not application tag. – Cuồn Yết Jun 26 '13 at 18:03
  • I don't know what it is then, everything seems fine from what you've described. Maybe edit your post and add your `manifest.xml`? We need more clues :) – Ken Wolf Jun 26 '13 at 18:05
  • @CuồnYết, you must declare `screeSize` too if your declared target API is 13 or higher. – yugidroid Jun 26 '13 at 18:11
  • One more thing I thought of (long shot) - make sure there's not some other error preventing you from building and that you are actually testing the latest version of your app. Make a small change to a string or something and see if that gets through. – Ken Wolf Jun 26 '13 at 18:33
1

Everything seems okay, except one thing! I don't know if it's your case or not, but you should read the documentation :D

You will have to declare screeSize if you applications targets API level 13 or higher.

If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

Let me know it this resolves your issue.

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • thanks yugidroid. my applications targets API level is 17. I have added the "screenSize" configuration, but it does not work. the activity is still in landscape. – Cuồn Yết Jun 26 '13 at 18:12