Screen setup Orientation in AndroidManifest.xml not working

I have a simple Android test project for Android. In my AndroidManifest.xml, I already installed

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

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

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

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

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 valid onCreate 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 gets weirder, the isLandscape variable is True, but sometimes it's False. It seems like a mistake.

+4
source share
2 answers

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

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

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 place it for each Activity that you define in your manifest.xml

+8
source

Everything seems good, except for one! I don’t know if it’s your business or not, but you should read the documentation : D

You need to declare screeSize if your applications are targeting API level 13 or higher.

If your application targets API level 13 or higher (as indicated in minSdkVersion and targetSdkVersion), then you must also declare the screenSize configuration, as it also changes when the device switches between portrait and landscape orientation.

Let me know that this solves your problem.

+1
source

Source: https://habr.com/ru/post/1488344/


All Articles