80

How to fix layout orientation to portrait and do not allow changing from portrait to landscape during run time?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Harinder
  • 11,776
  • 16
  • 70
  • 126

7 Answers7

163

In your AndroidMainfest.xml file find the tags of the activities you wish to lock to a given rotation, and add this attribute:

android:screenOrientation="portrait"
Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • @AlekseyTimoshchenko Can you describe how it doesn't work? – parvus Sep 27 '16 at 07:38
  • 3
    @parvus for me, when i added this attribute `android:screenOrientation="portrait"` in manifest according the answer i still can make rotation... eventually i found solution in next , i have added this line `setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);` on `onCreate();` of each activity – Sirop4ik Sep 27 '16 at 08:55
  • Hi Could you take a look at my issue https://stackoverflow.com/q/74940364/10110990 – Merlin Jeyakumar Dec 28 '22 at 12:41
24

Use setRequestedOrientation() as shown:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.main);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
David Choe
  • 241
  • 1
  • 2
  • Personally I prefer this, If you want to change the Orientation this is really handy, otherwise use XML if your only ever going to place the device in that orientation. – Chris.Jenkins Nov 06 '12 at 13:16
16

in your activity parameters in Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.statepermit" android:versionCode="1" android:versionName="1.0">
    <application android:icon="@drawable/stateheader" android:label="@string/app_name">
        <activity android:name=".statepermit" android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="7" />

</manifest>

android:screenOrientation="portrait"

Stone Edge
  • 52
  • 11
Hardik Gajjar
  • 5,038
  • 8
  • 33
  • 51
3
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

before

setContentView(R.layout.main);
Real Hyder
  • 547
  • 5
  • 3
3

If you want to freeze orientation at runtime, then you you can implement this:

Android: Temporarily disable orientation changes in an Activity

I use a similar approach and it works perfectly.

Community
  • 1
  • 1
user695977
  • 91
  • 1
  • 4
2

If you want to fix the orientation of one Activity in your project, you have to open the Manifest.xml and put in the section of parameters of the desired Activity (before the closure of the first tag < activity…> ):

android:screenOrientation="portrait" if you want VERTICAL fixed orientation

android:screenOrientation="landscape" if you want HORIZONTAL fixed orientation

Fra
  • 49
  • 4
  • Hi can you, help me in this https://stackoverflow.com/questions/74940364/android-13-landscape-orientation-are-not-actually-landscape – Merlin Jeyakumar Dec 29 '22 at 05:22
2

In your AndroidMainfest.xml just write this in your activity you declare,

If you want in layout vertical than use

android:screenOrientation="portrait"

If you want in layout landscape than use

android:screenOrientation="landscape"
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142