Force an Android activity to always use landscape mode
Asked Answered
X

16

162

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.

Xyloid answered 27/1, 2010 at 21:14 Comment(0)
C
202

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.

Catlike answered 27/1, 2010 at 21:55 Comment(3)
Why exactly is the configChanges attribute needed? It seems to work on my non-keyboard phone without it... In what situations will the orientation change to portrait if you have screenOrientation="landscape" but omit the configChanges?Valorie
in new versions you should also specify screenSize : 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.Hopeh
how to force an built-in activity like system image select library in landscape only or portrait only?Duer
B
124

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.

Burgh answered 27/1, 2010 at 21:47 Comment(1)
@Yegor Not always. From android docs: If the activity is currently in the foreground or otherwise impacting the screen orientation, the screen will immediately be changed (possibly causing the activity to be restarted)Frankhouse
B
44

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

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bask answered 22/4, 2012 at 17:30 Comment(2)
the manifest method in the other answers didn't seem to work but @Bask yours didCampion
how is this different from haseman's answer? ;)Cementum
T
33

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"] >  
Transgression answered 27/1, 2010 at 21:24 Comment(0)
A
29

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);
Aleron answered 25/1, 2016 at 16:3 Comment(0)
L
11

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"/>
Loeb answered 11/10, 2011 at 5:28 Comment(1)
"orientation" If your application targets API level 13 or higher then you should also declare the "screenSize"Pinot
S
9

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"
Syndactyl answered 16/3, 2015 at 19:45 Comment(0)
D
5

This works for Xamarin.Android. In OnCreate()

RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
Damales answered 23/8, 2016 at 23:18 Comment(0)
S
4

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.

Sherwood answered 12/9, 2012 at 10:22 Comment(0)
T
3

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" />

Tieck answered 24/1, 2011 at 21:46 Comment(1)
im not the asker, but maybe he wants to force programatically because he wants to force it at a certain time?Vessel
F
2

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>
Frolick answered 9/5, 2020 at 11:19 Comment(0)
E
1

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.

Emmieemmit answered 26/8, 2014 at 19:26 Comment(1)
I disagree. Doing anything in code as opposed to via a manifest is always my preferred method. Based on the single responsibility principle the manifest should only be concerned with application level properties, permissions and API bindings. I actually disagree with the <activity> tag being present at all in an application manifest and so I never put it in there (and it works fine). How an activity is presented is obviously a presentation concern. Your activity should be solely responsible for how it is presented and so I usually put all presentation logic in the view/activity code.Damales
W
0

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.

Wilkens answered 15/6, 2013 at 16:3 Comment(6)
Bad practice alert! This has nothing to do with forcing the activity in landscape mode. You allways need to implement the storing and the recreation of the state of your activity. That is basic Android Development knowledge. Don't solve that with this hacky fix. The activity can allways be closed by the system if it needs more memory.Disreputable
@Roel, well it's the only way to prevent the activity to rotate back to portrait when for example you rotate device. It prevents that these configchanges affect the activity.Wilkens
Sorry for little bit grumpy message, but I tested it and I don't all these values for landscape mode. When you add all these values it means you handle the changes yourself in onConfigurationChanged() instead of that the activity is just restarted (and you have to implement that!) but if you force landschape a rotation will not happen.Disreputable
Hi, some of them are maybe not needed, but omitting all of them will result in weird rotation issues. I can't test now, but when I was developing a app that needed to be in landscape all the time, only using orientation|screensize was not sufficient in all cases. It would autorotate back to portrait.Wilkens
These configchanges are also used in Google Mobile Ads and other Ad SDKs. That's where I got them from at that time.Wilkens
It is not always wrong to use them but you need to remember that you always have to implement saving and restoring state when your activity restarts so the user does not notice it when that happens.Disreputable
B
0

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

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Balderas answered 18/5, 2017 at 21:23 Comment(0)
K
0

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

Kosse answered 27/5, 2020 at 5:40 Comment(0)
K
-27

Press CTRL+F11 to rotate the screen.

Khat answered 18/10, 2012 at 12:26 Comment(2)
They are wanting to now how do set the default orientation of an app, not how the screen can be rotated.Raynata
CTRL+f12 for emulator.Mallory

© 2022 - 2024 — McMap. All rights reserved.