16

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

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
hullunist
  • 1,117
  • 2
  • 11
  • 31

6 Answers6

23

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...

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • 1
    This worked. tried all the other solutions which didn't work. Wonder why – hullunist Apr 13 '16 at 12:25
  • Setting this via the activity class prevents landscape from being an option, the manifest will provide the default, or hinted, preference but will not prevent it from happening – SushiHangover Apr 13 '16 at 12:28
  • @fbueckle other options surely works with Native android. :) may be because of XAMARIN it didn't work the way it is. – MKJParekh Apr 13 '16 at 12:29
  • 1
    @MKJParekh one of the issues is that Xamarin does not currently handle Android classing naming via the dot name convention that is usually the standard way all Android devs are used too . This is also an issue in tagging class name for items like a 3rd-party Launcher; you have to add the package name AND the class name. So in the end "easier" to tag the activity since a package name change will break your hard-code manifest class names ;-/ – SushiHangover Apr 13 '16 at 12:32
4

Android Screen Orientation Change (Portrait- Landscape)

1. Lock screen orientation change in Android

Your screen will always display in portrait mode, when you rotate your device, no changes will apply for the current activity.

android:screenOrientation="portrait"

if you want to lock the whole application screens. you should write the above code to all activities of the application.

<?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:screenOrientation="portrait"
    android:configChanges="keyboard|keyboardHidden|orientation" />
  </application>
</manifest>

2. Lock screen orientation (screen rotation) programmatically in Android

This option is little tricky. What we need to do is, we will get the screen orientation change (rotation change) event, then both for landscape and portrait mode event, we will set ether landscape or portrait mode as per our requirement. Below is the code. We will set portrait mode always, so the screen will always stay in portrait mode.

// Check screen orientation or screen rotate event here
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
 
        // Checks the orientation of the screen for landscape and portrait and set portrait mode always
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

3. Change screen orientation programmatically at anytime in Android

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

4. How to get screen orientation at runtime

//Get current screen orientation
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getOrientation(); 
         switch(orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;                  
        }

5. Find screen orientation change event in Android

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
 
        // Checks the orientation of the screen for landscape and portrait
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        }
    }
Community
  • 1
  • 1
Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
1
<activity android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation"
android:screenOrientation="portrait" />

Use the above coding add android:screenOrientation="portrait" in <activity> tab.

Praveen Kumar
  • 547
  • 1
  • 7
  • 33
0

Add this line,

<activity android:name=".MainActivity"
 android:screenOrientation="portrait"
 android:configChanges="keyboard|keyboardHidden|orientation" />
Pradeep Gupta
  • 1,770
  • 1
  • 9
  • 23
0

Try 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|screenSize"
            android:screenOrientation="portrait" />
  </application>
</manifest>
Slobodan Antonijević
  • 2,533
  • 2
  • 17
  • 27
0

Use 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:screenOrientation="portrait"
    android:configChanges="keyboard|keyboardHidden|orientation" />
  </application>
</manifest>

OR

If you want to change the screen Orientation at Run time add following

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//For Portrait

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//For Lndscape
Rakshit Nawani
  • 2,604
  • 14
  • 27