How do we set Mobile phones to Portrait only and allow tablets to switch between portrait and landscape?
I am using Ionic/Cordova.
Currently I have configured to set the app to be in portrait mode in config.xml using the following property
<preference name="orientation" value="portrait" />
Using the ionic native and Cordova plugin, we can access the PORTRAIT and LANDSCAPE methods to lock the screen. Similarly, we can access the unlock method to unlock the screen orientation in Ionic. Let us combine all the methods and functions to give you the final code of this tutorial. Open home.page.ts file, and update with the given below code:
The orientation is in the secondary portrait mode. The orientation is in the primary landscape mode. The orientation is in the secondary landscape mode. The orientation is either portrait-primary or portrait-secondary (sensor). The orientation is either landscape-primary or landscape-secondary (sensor).
1. Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. 2. Setting programmatically in Java Note : In Activity you should call this before setContentView in onCreate () method.
There are 2 ways to achieve Activity level Orientation control. 1. Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. 2. Setting programmatically in Java
You could use isTablet plugin in conjunction with screen orientation plugin, something like:
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.isTablet){
screen.unlockOrientation();
}else{
screen.lockOrientation('portrait');
}
});
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With