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

Viewed 288089

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

9 Answers

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);

Try this: (if SDK 23 & above)

Add your AndroidManifest.xlm;

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

like this.

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!

The accepted answer is correct. However, if you happen to be doing cross-platform development in C# using Uno Platform the Activity configuration is defined in an Activity attribute in the class. I'd expect this is the same if you're doing Xamarin or Xamarin Forms development.

Set: ScreenOrientation = ScreenOrientation.Portrait in the attribute parameters.

Here's an example within my MainActivity.cs class-level attribute:

[Activity(MainLauncher = true,
        ConfigurationChanges = global::Uno.UI.ActivityHelper.AllConfigChanges,
        WindowSoftInputMode = SoftInput.AdjustPan | SoftInput.StateHidden,
        ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Windows.UI.Xaml.ApplicationActivity { }

#unoplatform #xamarin #csharp #windows #crossplatform

Related