Get Device Orientation and Set Preferred Orientation in React Native

Get Device Orientation and Set Preferred Orientation in React Native

Hello guys, This is an Example to Get Device Orientation and Set Preferred Orientation in React Native. In this example we will see how can you control screen orientation and how can you get the rotation of the screen. Sometimes it becomes very much important to manage the orientation of the screen and if it is done properly then it will provide a user-friendly experience to your app users which is very much important for a developer.

If I have to mention the most general example of screen rotation or the orientation of the screen then a video playing application like Youtube is the best example, it uses the screen very well using the orientation as users are very comfortable with portrait mode while searching and after playing a video it provides the landscape option to use your full screen.

In this example, we are going to see how to control screen rotation/orientation using react-native-orientation library.

You can also see How to Disable Screen Rotation in React Native.

Get Device Orientation

To get the orientation of the device react-native-orientation library provides two different categories:

1. Orientation

In the normal orientation we will get the following orientation as a response:

  • LANDSCAPE
  • PORTRAIT
  • PORTRAITUPSIDEDOWN
  • UNKNOWN

To get the Orientation any time use

To add the Orientation Listener for the change in the orientation which will be triggered automatically when the orientation of the device changes

To remove the Orientation Listener use

2. Specific Orientation

In the specific orientation we will get the following orientation as a response:

  • LANDSCAPE-LEFT
  • LANDSCAPE-RIGHT
  • PORTRAIT
  • PORTRAITUPSIDEDOWN
  • UNKNOWN

To get the Specific Orientation any time use

To add the Specific Orientation Listener for the change in the orientation which will be triggered automatically when the orientation of the device changes

To remove the Specific Orientation Listener use

Set Preferred Orientation

Here are the all possible orientation which you can set using this library:

1. To Lock the View to Portrait Mode

2. To Locks the View to Landscape Mode

3. To Locks the View to Right Landscape Mode

4. To Locks the View to Left Landscape Mode

5. Unlocks any Previous Locked Orientations

I think this is enough to know about the library, now let’s move towards the example. In this example, we are going to see 7 buttons on the screen in which 2 buttons will be used to get the orientation and specific orientation of the device at any time and 5 buttons to set the preferred orientation.
We have also added orientation listeners for the screen so that the current orientation of the device will be visible on the screen.
Now let’s get started with the example.

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependency

To use Orientation we have to install react-native-orientation dependency.

To install the dependencies open the terminal and jump into your project

Now install the dependency

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example we need to install the pods for react-native-orientation.

Please use the following command to install CocoaPods

Code to Get Device Orientation and Set Preferred Orientation in React Native

Now Open App.js in any code editor and replace the code with the following code.

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Img   Img   Img
Img   Img   Img
Img   Img   Img

This is how you can Get Device Orientation and Set Preferred Orientation in React Native. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

2 thoughts on “Get Device Orientation and Set Preferred Orientation in React Native”

  1. Hello !
    Great article, I tried and follow it but I must have been wrong, I’ve got this error. Maybe you have an idea of where it comes from ?

    TypeError: null is not an object (evaluating ‘Orientation.initialOrientation’)
    – node_modules\react-native-orientation\index.js:98:23 in getInitialOrientation
    * App.js:27:20 in componentDidMount
    – node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:15
    036:10 in commitLifeCycles
    – node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:16
    636:8 in commitAllLifeCycles
    – … 21 more stack frames from framework internals

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.