Lock or change orientation on Android using NativeScript
Easily lock or change the orientation of your app on Android using NativeScript
We need to lock or change the device orientation to see a video or play a game etc. You can do so in Android using NativeScript as follows:
function lockOrientation(orientation) {
var activity = application.startActivity;
switch (orientation) {
case 'unlocked':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
break;
case 'portrait-primary':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case 'portrait-secondary':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
break;
case 'landscape-primary':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
case 'landscape-secondary':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case 'portrait':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
break;
case 'landscape':
activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
break;
default: activity.setRequestedOrientation(android.content.pm.ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
break;
}
}
When ever you need to set or remove the orientation call the function like this:
lockOrientation('unlocked');
or
lockOrientation('landscape');