How to fix layout orientation to vertical?

AndroidAndroid Orientation

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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"

Solution 2 - Android

Use setRequestedOrientation() as shown:

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Solution 3 - Android

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"

Solution 4 - Android

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

https://stackoverflow.com/questions/3611457/android-temporarily-disable-orientation-changes-in-an-activity/3611554#3611554

I use a similar approach and it works perfectly.

Solution 5 - Android

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

before

setContentView(R.layout.main);

Solution 6 - Android

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

Solution 7 - Android

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"

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
QuestionHarinderView Question on Stackoverflow
Solution 1 - AndroidJim BlacklerView Answer on Stackoverflow
Solution 2 - AndroidDavid ChoeView Answer on Stackoverflow
Solution 3 - AndroidHardik GajjarView Answer on Stackoverflow
Solution 4 - Androiduser695977View Answer on Stackoverflow
Solution 5 - AndroidReal HyderView Answer on Stackoverflow
Solution 6 - AndroidFraView Answer on Stackoverflow
Solution 7 - AndroidAmit VaghelaView Answer on Stackoverflow