91

I am new to android environment. I know iOS can be done in Xcode to disable device orientation. How can I disable landscape mode or any orientation mode in React Native Android?

Thanks.

Ivan Chernykh
  • 41,617
  • 13
  • 134
  • 146
sungl
  • 1,155
  • 1
  • 7
  • 16

9 Answers9

153

Add android:screenOrientation="portrait" to the activity section in android/app/src/main/AndroidManifest.xml file, so that it end up looking like this:

<activity
    android:name=".Activity"
    android:label="Activity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

There are several different values for the android:screenOrientation property; for a comprehensive list take a look at the following: https://developer.android.com/guide/topics/manifest/activity-element.html

peterp
  • 3,145
  • 1
  • 20
  • 24
APCM
  • 1,704
  • 1
  • 11
  • 24
  • Do we have to do it for every activity? – Ashish Mar 15 '17 at 15:53
  • 30
    BE CAREFUL, If you're using ReactNativeNavigation then this doesn't work for you. Set "orientation: 'portrait'," in your appStyle in startSingleScreenApp or startTabBasedApp. Take a look at [this thread](https://github.com/wix/react-native-navigation/issues/349), it may help. – Bat Oct 09 '17 at 04:54
  • 4
    You really want to use `sensorPortrait` instead of `portrait` so that it can be used "upside down" as well. And, if you're targeting API 18+, you should use `userPortrait`, as it will respect if the user has locked orientation. See the docs for more details: https://developer.android.com/guide/topics/manifest/activity-element.html#screen – Joshua Pinter Nov 12 '17 at 03:45
  • 6
    @Bat ReactNativeNavigation apparently has changed since your comment, and setting `android:screenOrientation` as suggested in the Answer works fine now. – Arnold Schrijver Jul 26 '19 at 15:25
  • how can i do it if i want landscape for tab and portrait for mobile device. please help. @Gem Ubaldo – Samiksha Jagtap Jun 01 '21 at 04:53
48

http://developer.android.com/guide/topics/manifest/activity-element.html

add android:screenOrientation="portrait" to your activity xml

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
33

As @morenoh149 stated above the property name and value to do this is android:screenOrientation="portrait". React Native generates a file called AndroidManifest.xml in your project directory under the Android folder. Within that xml file under the tag manifest/application/activity you would add the line android:screenOrientation="portrait"

An example is shown below

<uses-permission android:name="android.permission.INTERNET" />

<application
  android:allowBackup="true"
  android:label="@string/app_name"
  android:icon="@mipmap/ic_launcher"
  android:theme="@style/AppTheme">
  <activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait" 
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>

bret
  • 796
  • 9
  • 5
17

Harry Moreno's comment is correct. Add it to android/app/src/main/AndroidManifest.xml in the 'activity' section. Also change

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

to android:configChanges="keyboard|keyboardHidden|screenSize"

removing orientation so it doesn't conflict with the new line added, android:screenOrientation="portrait"

mixophrygian
  • 659
  • 6
  • 10
  • 1
    There's nothing wrong with what @mixophrygian says if you want to do what OP wants. Which is to completely disable landscape view. However, if somewhere in your app you want the user to view things landscape then don't remove it. There won't be any conflict (there's also no conflict if you leave this in regardless). – Mix Master Mike Mar 06 '19 at 23:38
15

2017 Update

In addition to the solutions posted above if you're using Expo to build your app (as is currently recommended in official guides on React Native Blog) then all you need is to set orientation in app.json to "portrait" or "landscape" and make it work on both iOS and Android at the same time with no need to edit iOS/Android-specific XML configuration files:

"orientation": "portrait"

Example:

{
  "expo": {
    "name": "My app",
    "slug": "my-app",
    "sdkVersion": "21.0.0",
    "privacy": "public",
    "orientation": "portrait"
  }
}

You can also use this at runtime:

ScreenOrientation.allow()

Example:

ScreenOrientation.allow(ScreenOrientation.Orientation.PORTRAIT);

More details:

For more info see: how to disable rotation in React Native?

rsp
  • 107,747
  • 29
  • 201
  • 177
13

The accepted answer doesn't work if you're using react-native-navigation. Use this instead:

Navigation.setDefaultOptions({
    layout: {
        orientation: ["portrait"],
    },
});

Check the docs here.

romin21
  • 1,532
  • 1
  • 14
  • 35
4

You can simply the logic by adding a lifecycle callback in ReactApplication class:

public class MyApplication extends Application{

@Override
    public void onCreate() {
        super.onCreate();  

  registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {
                // Here we specify to force portrait at each start of any Activity
                activity.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {

            }
        });
}

So you don't need to configure AndroidManifest

odemolliens
  • 2,581
  • 2
  • 32
  • 34
  • This fit my needs to lock orientation only in smartphone devices and remain free orientation on tablets, I just did a device check before activity.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); – Fábio Palmeira May 21 '17 at 14:45
  • This was the only solution that worked for me. Updating `AndroidManifest.xml` had no effect; not sure if I just wasn't publishing it properly. I found I had to call `setRequestedOrientation` in `onActivityResumed` and `onActivityStarted` too. – z0r Sep 19 '17 at 01:02
3

Just open your android/app/src/main/AndroidManifest.xml

And add this line android:screenOrientation="portrait" and android:configChanges="keyboard|keyboardHidden|orientation|screenSize inside activity section

It looks something like that

 <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:screenOrientation="portrait"
      android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
     .....your other stuff
 </activity>

If it does not work than you can use react-native-orientation

0

For React Native Navigation it could be fixed with defining orientation as described here:

Navigation.setDefaultOptions({
  layout: {
    orientation: ['portrait']
  }
});
Ivan Verevkin
  • 127
  • 1
  • 7