xerotolerant xerotolerant - 29 days ago 30
Android Question

How to set orientation in nativescript

Hello I would like to know how to set device orientation in nativescript.
Specifically I want the application that I am writing to stay in the same orientation (portrait) at all times so that rotating the device does not cause it to go into landscape.

I tried the nativescript-orientation plugin and setOrientation.

var orientation = require('nativescript-orientation');
console.log(JSON.stringify(orientation));// outputs JS: {}
orientation.setOrientation("portrait");


However I get the error "Cannot read property setOrientation of undefined.
tns plugin list shows that the plugin is installed. Also I tried removing the
platforms/android
directory and running
tns platform add android
with the same result.

I also tried putting various combinations of
android:screenOrientation="portrait"
into AndroidManifest.xml without success.

AndroidManifest.xml from inside App_resources looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"/>

<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="__APILEVEL__"/>

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

<application
android:screenOrientation="portrait"
android:name="com.tns.NativeScriptApplication"
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name="com.tns.NativeScriptActivity"
android:label="@string/title_activity_kimera"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/LaunchScreenTheme">
<meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.tns.ErrorReportActivity"/>
</application>
</manifest>

Answer

My app is portrait orientation only. For android, you just need to add android:screenOrientation="portrait" in AndroidManifest.xml inside the activity tags. For iOS, open Xcode -> Select project on project navigator (left panel) -> Select target in middle panel -> Choose "General" tab -> Tick only "Portrait" in Deployment info section

Comments