I want my android application to be only run in portrait mode?

AndroidScreen Orientation

Android Problem Overview


I want my android application to be only run in portrait mode? How can I do that?

Android Solutions


Solution 1 - Android

In the manifest, set this for all your activities:

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

Let me explain:

  • With android:configChanges="orientation" you tell Android that you will be responsible of the changes of orientation.
  • android:screenOrientation="portrait" you set the default orientation mode.

Solution 2 - Android

In Android Manifest File, put attribute for your <activity> that android:screenOrientation="portrait"

Solution 3 - Android

There are two ways,

  1. Add android:screenOrientation="portrait" for each Activity in Manifest File
  2. Add this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); in each java file.

Solution 4 - Android

in the manifest:

<activity android:name=".activity.MainActivity"
        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>

or : in the MainActivity

@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Solution 5 - Android

Old post I know. In order to run your app always in portrait mode even when orientation may be or is swapped etc (for example on tablets) I designed this function that is used to set the device in the right orientation without the need to know how the portrait and landscape features are organised on the device.

   private void initActivityScreenOrientPortrait()
    {
        // Avoid screen rotations (use the manifests android:screenOrientation setting)
        // Set this to nosensor or potrait
        
        // Set window fullscreen
    	this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    	
    	DisplayMetrics metrics = new DisplayMetrics();
    	this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        
         // Test if it is VISUAL in portrait mode by simply checking it's size
        boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels ); 

    	if( !bIsVisualPortrait )
    	{ 
        	// Swap the orientation to match the VISUAL portrait mode
    		if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
    		 { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
    		else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
    	}
    	else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }
    	
    }

Works like a charm!

NOTICE: Change this.activity by your activity or add it to the main activity and remove this.activity ;-)

Solution 6 - Android

Try this: (if SDK 23 & above)

Add your AndroidManifest.xlm;

    <activity android:name=".YourActivity"
        android:screenOrientation="locked"/>

like this.

Solution 7 - Android

in new SDk of android (24 and above) you can use in Manifest.xml in activity tag as you want:

<activity
            android:name=".feature.main.MainActivity"
            ********* android:screenOrientation="locked" ******
            android:configChanges="uiMode"
            android:windowSoftInputMode="adjustPan"
            android:exported="true">

and it work!

Solution 8 - Android

I use

 android:screenOrientation="nosensor"

It is helpful if you do not want to support up side down portrait mode.

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
QuestionJamesView Question on Stackoverflow
Solution 1 - AndroidCristianView Answer on Stackoverflow
Solution 2 - AndroidPraveenView Answer on Stackoverflow
Solution 3 - AndroidAndroid Code SolutionView Answer on Stackoverflow
Solution 4 - AndroidMeysam KeshvariView Answer on Stackoverflow
Solution 5 - AndroidCodebeatView Answer on Stackoverflow
Solution 6 - AndroidMustafa Tolga DalbudakView Answer on Stackoverflow
Solution 7 - AndroidSana EbadiView Answer on Stackoverflow
Solution 8 - Androiduser2305886View Answer on Stackoverflow