Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android + XAMARIN + Force screen to stay in "Portrait" mode (using AndroidManifest)

I read some of the answers to this problem on here but somehow I don't get it to work.

My AndroidManifest.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mWidas2.mWidas2" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
  <uses-sdk />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <application android:label="WQS" android:icon="@drawable/temporaryIcon">
    <activity android:name=".MainActivity"
    android:configChanges="keyboard|keyboardHidden|orientation" />
  </application>
</manifest>

I am still able to switch to landscape mode and since the layout gets pretty ugly in landscape mode I want to avoid that. Help much appreciated.

Might be of any importance: I'm developing the App with the latest Xamarin

like image 904
hullunist Avatar asked Apr 13 '16 12:04

hullunist


People also ask

How do I force an app into portrait mode?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”


1 Answers

You can tag your activity to be portrait using a class attribute:

[Activity (Label = "MyMainScreen", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class Activity1 : Activity

This is the only way that will force the activity to always be portrait based due to auto-generated class names produced by Xamarin without hard-coding your class names.

Update:

I've gotten a lot of IM questions concerning this Q/A, so I'm expanding the answer on why using the manifest method does not work.

Xamarin auto-generates a fully qualified class name for your activities that do NOT have a Name attribute assigned to your activity class.

If you look at a signed/generated manifest from an Xamarin Android .apk you will see a class name like:

md5d2519388ea1895e3e3594794d2e0c4ce.MainActivity

Since you are tagging your manifest with a dot name class identifier that will get the package name prefixed to it:

<activity android:name=".MainActivity"

The fully qualified class names do not match since a class name that begins with a period will get prefix with the package name and those never match the auto-generated class name.

You can work around this auto-generated fully qualified class name by using the Name element in your Activity attribute to prevent the auto-generated name from being created, i.e.:

[Activity(Label = "PlayScriptStarling", Name = "com.sushihangover.playscriptstarling.MyBigBadGameEveryOneShouldPlay", MainLauncher = true, Icon = "@mipmap/icon")]

Now, assuming your package name is "com.sushihangover.playscriptstarling", using:

<activity android:name=".MyBigBadGameEveryOneShouldPlay"

in your manifest will work as the class name, once expanded in the manifest matches the one in your code.

The issue is then if your package name changes you break your dot class names in your manifest. So using ScreenOrientation = ScreenOrientation.Portrait is a cleaner way of assigning orientation that does not require you to assign fully qualified class name on every one of your activities and does not break if the package name changes.

Note: The Name element within the Activity class attribute does NOT support dot class names and requires a fully expanded class name to be used. This has been submitted to Xamarin as a feature request...

like image 128
SushiHangover Avatar answered Oct 22 '22 05:10

SushiHangover