Home > Software engineering >  React native android autorotate issue
React native android autorotate issue

Time:01-13

I want my application should be fixed to portrait orientation. but when I enable the auto-rotate in mobile, my application can able to rotate portrait and landscape. even I have added the android:screenOrientation="portrait" and tools:ignore="LockedOrientationActivity" in the AndroidManifest file, it still allows rotation of the application.

Can anyone please help to fix my application has portrait orientation even auto-rotate enabled? I have searched on google. but no clue. please suggest to me some idea to fix this issue.

AndroidManifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
  package="xxxxxxxxxxxxxx">

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

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
      android:largeHeap="true"
      android:usesCleartextTraffic="true">
             <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustPan" android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity 
      android:screenOrientation="portrait"      android:name="com.facebook.react.devsupport.DevSettingsActivity" />
      
    </application>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_CALENDAR" />
</manifest>

CodePudding user response:

lets use this lib react-native-orientation-locker, you can change to any orientation you want.

import Orientation from 'react-native-orientation-locker';

Orientation.lockToPortrait();

CodePudding user response:

another way in MainActivity.java

import android.content.pm.ActivityInfo; // <-- add this line

...

@Override
protected void onCreate(Bundle savedInstanceState) {
  
  ...
   
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); // <-- add this line
  
  ...
}
  •  Tags:  
  • Related