PhoneGap - Preventing Screen Orientation Changes - android

I've used a plugin to try and lock the screen orientation within my PhoneGap app:
cordova-plugin-screen-orientation
Within my app page I have the following code extract:
// Once DOM is loaded...
$(document).on('pagecreate', function() {
window.screen.lockOrientation('portrait'); // Seems to set variable but not LOCK screen?
var orient = screen.orientation; // Works
alert(orient); // Works
window.addEventListener("orientationchange", function(){ // Works
alert(screen.orientation); // e.g. portrait // Works
});
... Other code etc.
As you can see from the comments the plugin seems to be installed correctly as I am successfully calling portrait-primary, landscape-secondary etc. as I rotate my phone. The issue seems to be the function isn't LOCKING the screen?
Any help would be GREATLY appreciated!

Try setting this attribute in your config.xml.
<preference name="Orientation" value="portrait" />

Related

Can't hide Intel XDK splash screen

I have created a small game and created build using Intel XDK. I installed it in device and can see the splash screen. I tried what is mentioned in other topics but unable to remove the splash screen.
function onDeviceReady(){
if( navigator.splashscreen && navigator.splashscreen.hide ) {
navigator.splashscreen.hide();
}
if( window.intel && intel.xdk && intel.xdk.device ) {
if( intel.xdk.device.hideSplashScreen ) {
intel.xdk.device.hideSplashScreen();
}
intel.xdk.device.setRotateOrientation("landscape");
intel.xdk.device.setAutoRotate(false);
intel.xdk.device.hideStatusBar();
}
}
document.addEventListener("intel.xdk.device.ready", onDeviceReady, false);
This is how it looks initially when I launch the app.
I have this plugin added in the project
cordova-plugin-splashscreen
Please help me resolve this issue.
Received this answer on XDK forum
The purpose of the splashscreen is to hide the details of the
initialization process (even before your device ready fires). The
splashscreen you see is the default cordova splash screen, you can
replace it by your own graphics. You can upload custom splashscreen
through Launch Icons and Splash screens on Projects page. The
hidesplashscreen() method will hide the splashscreen that is displayed
at the initialization and the contents of your app will appear (which
happens when device is ready).

IOS Cordova Lock SplashScreen Orientation

Background
I'm using Cordova to deploy javascript to iOS devices. Along with this, I'm using the CDVSplashScreen plugin in order to show my own splashscreen.
What I need help with
I want to be able to lock only the Launch/Splash Screens and then allow the app to reorient as it will for all other views.
What I've tried
I've added the following to my config.xml file:
<preference name="SplashScreen" value="screen" />
<preference name="orientation" value="portrait" />
<preference name="SplashScreenDelay" value="1000000" />
And I added the following code to both the CDVSplashScreen.m and CDVViewController+SplashScreen.m files:
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
The Results:
What I've added to the config.xml file appears to have locked the Launch Screen in portrait mode...however, after the SplashScreen appears, the view will rotate freely to landscape mode.
I don't believe that the supportedInterfaceOrientations function is doing anything at all where it's at - it only works in the mainViewController (but then the entire app is stuck in Portrait mode).
My Plea
Has anyone used this unique conglomeration of plugins before with the same requirement of a locked SplashScreen before? How did you solve it?
I haven't checked, but you could try the following:
Lock the orientation in Xcode to your needs. (Xcode -> General -> Deployment info)
Put an event handler in your javascript:
window.addEventListener("orientationchange", function() {
// Do some DOM-/CSS-manipulation depending on the value in window.orientation
}, false);
Or you could use CSS to make changes:
#media screen and (orientation: portrait) {
/* rotate your container */
}
#media screen and (orientation: landscape) {
/* rotate your container */
}
But I'm not sure that the device is firing the event.
Thank you guys for your suggestions. I ended up setting the orientation in the following function in the MainViewController:
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.acceptedOrientation = UIInterfaceOrientationMaskPortrait;
}
return self;
}
This seemed to lock the SplashScreen in portrait mode for me.

Hide splashscreen phonegap v3

I am trying to make sure that my splash screen does not show in iOS phonegap v3
I have tried the following as suggested in their documentation:
Which includes installing the splash plugin
I immediately call hide
navigator.splashscreen.hide()
project layout
¬ res
¬ icon
config.xml
<gap:plugin name="org.apache.cordova.splashscreen" />
<feature name="SplashScreen">
<param name="android-package" value="org.apache.cordova.splashscreen.SplashScreen" />
<param name="ios-package" value="CDVSplashScreen" />
</feature>
Notice there is no splash folder. Yet, the splash screen will always show and also display the default phonegap splash screen rather than skipping it.
Worth mentioning I am building via https://build.phonegap.com
Are you sure that the deviceready event is firing before you are calling navigator.splashscreen.hide()? Calling .hide() immediately after that event is fired should yield the result you are looking for.
Also, you can try replacing the splashscreen plugin altogether for cordova's new alternative:
cordova.exec(null, null, “SplashScreen”, “hide”, [])
The cordova.exec command hooks straight into the different devices native environments.
The parameters explained in more detail:
function(winParam) {} - Success function callback. Assuming your exec call completes successfully, this function will be invoked (optionally with any parameters you pass back to it)
function(error) {} - Error function callback. If the operation does not complete successfully, this function will be invoked (optionally with an error parameter)
"service" - The service name to call into on the native side. This will be mapped to a native class.
"action" - The action name to call into. This is picked up by the native class receiving the exec call, and, depending on the platform, essentially maps to a class's method.
[/* arguments */] - Arguments to get passed into the native environment
I think - stress think - that you can't do this. You could fake it perhaps by using a splash screen of a single color that matches the color of your app.

locking screen orientation on mobile browser

I create a web page(chrome & safari) for mobiles (iphone & android), I want to lock the screen orientation in portrait mode.
Unlike mobile apps,there is no manifest file and activity as it a web page.
How to lock the orientation in mobiles using technologies (css/javascript/bootstrap/jquery) or any other?
I use a manifest file for my web app, which locks orientation for Chrome on my Android. For whatever reason, Safari gives their users the "right" to do this, but not the designers of the web app... Sort of feels like copyright infringement or something! ;) Don't get me started on Safari's disgraceful rewriting/rendering of input buttons!...
Anyways, back to the answer.
1) Include a link to your manifest within the head section of your page:
<link rel="manifest" href="http://yoursite.com/manifest.json">
2) Create your manifest file, "manifest.json"
{
"name":"A nice title for your web app",
"display":"standalone",
"orientation":"portrait"
}
3) Read more about manifests HERE
From my tests, assigning the screen.lockOrientation ( every browser versions ) to a var throw an illegal invocation error. Just use wind.screen.orientation.lock('landscape'); . It
EDIT: You can't use lock orientation on safari, cause it doesn't support fullscreen api at the moment http://caniuse.com/#feat=fullscreen . The lock orientation API NEED a fullscreen page to work. In Chrome, the window.screen.orientation.lock return a promise. So, AFTER you go fullscreen with the page, you can do something like this :
var lockFunction = window.screen.orientation.lock;
if (lockFunction.call(window.screen.orientation, 'landscape')) {
console.log('Orientation locked')
} else {
console.error('There was a problem in locking the orientation')
}
However, the lock orientation and fullscreen API are still experimental, not all browsers supports it.
The lockOrientation method locks the screen into the specified orientation.
lockedAllowed = window.screen.lockOrientation(orientation);
From the following code, you can check that orientation is locked or not.
var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;
if (lockOrientation("landscape-primary")) {
// orientation was locked
} else {
// orientation lock failed
}
see the following link, you will get idea from this.
https://developer.mozilla.org/en-US/docs/Web/API/Screen.lockOrientation
You can use:
screen.addEventListener("orientationchange", function () {
console.log("The orientation of the screen is: " + screen.orientation);
});
and
screen.lockOrientation('landscape');
Following: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Managing_screen_orientation

Black screen for Android app

I have added a splash screen for an Android app. It displays, but it shows a black screen for two seconds after showing the splash screen.
How can I fix this?
Based on your tags I assumed that you are facing issue for Android Phonegap app.
You have to close splash screen on device ready instead of giving specific time in loadUrl method.
Code Snippet:
super.setIntegerProperty("splashscreen", R.drawable.splash); // Display splash screen for android
this.setIntegerProperty("loadUrlTimeoutValue", 70000);
super.loadUrl("file:///android_asset/www/index.html",10000);// Give max time here
Hide splash screen in Phonegap onDeviceReady method :
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
cordova.exec(null, null, "SplashScreen", "hide", [])
}

Categories

Resources