Force an Android activity to always use landscape mode

AndroidLandscapeAndroid Orientation

Android Problem Overview


I am using the Android VNC viewer on my HTC G1. But for some reason, that application is always in landscape mode despite my G1 is in portrait mode. Since the Android VNC viewer is open source, I would like know how is it possible hard code an activity to be 'landscape'. I would like to change it to respect the phone orientation.

Android Solutions


Solution 1 - Android

Looking at the AndroidManifest.xml (link), on line 9:

<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden" android:name="VncCanvasActivity">

This line specifies the screenOrientation as landscape, but author goes further in overriding any screen orientation changes with configChanges="orientation|keyboardHidden". This points to a overridden function in VncCanvasActivity.java.

If you look at VncCanvasActivity, on line 109 is the overrided function:

@Override
public void onConfigurationChanged(Configuration newConfig) {
  // ignore orientation/keyboard change
  super.onConfigurationChanged(newConfig);
}

The author specifically put a comment to ignore any keyboard or orientation changes.


If you want to change this, you can go back to the AndroidManifest.xml file shown above, and change the line to:

<activity android:screenOrientation="sensor" android:name="VncCanvasActivity">

This should change the program to switch from portrait to landscape when the user rotates the device.

This may work, but might mess up how the GUI looks, depending on how the layout were created. You will have to account for that. Also, depending on how the activities are coded, you may notice that when screen orientation is changed, the values that were filled into any input boxes disappear. This also may have to be handled.

Solution 2 - Android

You can set the same data in your java code as well.

myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Other values on ActivityInfo will let you set it back to sensor driven or locked portrait. Personally, I like to set it to something in the Manifest as suggested in another answer to this question and then change it later using the above call in the Android SDK if there's a need.

Solution 3 - Android

In my OnCreate(Bundle), I generally do the following:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Solution 4 - Android

You can specify the orientation of an activity in the manifest. See here.

<activity android:allowTaskReparenting=["true" | "false"]
...
          android:screenOrientation=["unspecified" | "user" | "behind" |
                                     "landscape" | "portrait" |
                                     "sensor" | "nosensor"]
...
                                       "adjustResize", "adjustPan"] >  

Solution 5 - Android

In the manifest:

<activity  android:name=".YourActivity"
android:screenOrientation="portrait"
android:configChanges="orientation|screenSize">

In your activity:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.your_activity_layout);

Solution 6 - Android

The following is the code which I used to display all activity in landscape mode:

<activity android:screenOrientation="landscape"
          android:configChanges="orientation|keyboardHidden"
          android:name="abcActivty"/>

Solution 7 - Android

A quick and simple solution is for the AndroidManifest.xml file, add the following for each activity that you wish to force to landscape mode:

android:screenOrientation="landscape"

Solution 8 - Android

This works for Xamarin.Android. In OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;

Solution 9 - Android

That's it!! Long waiting for this fix.

I've an old Android issue about double-start an activity that required (programmatically) landscape mode: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Now Android make Landscape mode on start.

Solution 10 - Android

Arslan, why do you want to force orientation pro grammatically, though there's already a way in manifest <activity android:name=".youractivityName" android:screenOrientation="portrait" />

Solution 11 - Android

Add The Following Lines in Activity

You need to enter in every Activity

for landscape

android:screenOrientation="landscape"
tools:ignore="LockedOrientationActivity"

for portrait

android:screenOrientation="portrait"
tools:ignore="LockedOrientationActivity"

Here The Example of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="org.thcb.app">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity"
            android:screenOrientation="landscape"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".MainActivity2"
            android:screenOrientation="portrait"
            tools:ignore="LockedOrientationActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Solution 12 - Android

Doing it in code is is IMO wrong and even more so if you put it into the onCreate. Do it in the manifest and the "system" knows the orientation from the startup of the app. And this type of meta or top level "guidance" SHOULD be in the manifest. If you want to prove it to yourself set a break in the Activity's onCreate. If you do it in code there it will be called twice : it starts up in Portrait mode then is switched to Landscape. This does not happen if you do it in the manifest.

Solution 13 - Android

For Android 4.0 (Ice Cream Sandwich) and later, I needed to add these, besides the landscape value.

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

Using only keyboardHidden|orientation would still result in memory leaks and recreation of my activities when pressing the power button.

Solution 14 - Android

Use the ActivityInfo (android.content.pm.ActivityInfo) in your onCreate method before calling setLayout method like this

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Solution 15 - Android

use Only
android:screenOrientation="portrait" tools:ignore="LockedOrientationActivity"

Solution 16 - Android

Press CTRL+F11 to rotate the screen.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhap497View Question on Stackoverflow
Solution 1 - AndroidPulkit SethiView Answer on Stackoverflow
Solution 2 - AndroidhasemanView Answer on Stackoverflow
Solution 3 - AndroidanwarmaView Answer on Stackoverflow
Solution 4 - AndroidMichael KrauklisView Answer on Stackoverflow
Solution 5 - AndroidavisperView Answer on Stackoverflow
Solution 6 - AndroidMeghaView Answer on Stackoverflow
Solution 7 - AndroidjoshgoldeneagleView Answer on Stackoverflow
Solution 8 - AndroidComeInView Answer on Stackoverflow
Solution 9 - AndroidAndrewDCView Answer on Stackoverflow
Solution 10 - AndroidBlue MoonView Answer on Stackoverflow
Solution 11 - AndroidChetanView Answer on Stackoverflow
Solution 12 - AndroidRichieHHView Answer on Stackoverflow
Solution 13 - AndroidPeterdkView Answer on Stackoverflow
Solution 14 - AndroidYamikani SitaView Answer on Stackoverflow
Solution 15 - AndroidVibhu Vikram SinghView Answer on Stackoverflow
Solution 16 - AndroidYann Pascal DEUNGOUE DJOMENIView Answer on Stackoverflow