set non-scalable width for app on android tablets - android

I am working on an app for android tablets using phonegap. I am converting an existing webpage, so I would prefer to not change the css or html. I am testing using a galaxy tab (android 2.2)
I want to set the viewable width to be 800px and disable the user from zooming in/out.
If I use <meta name="viewport" content="width=800px>, then the width is set correctly but the user can still scale the page.
If I use <meta name="viewport" content="width=800px, user-scalable=no">, then the user can't scale the page, but the width will be smaller (ie. the page is zoomed in, and the user has to scroll around to view the page)
The only other thing I can think of is using <meta name="viewport" content="minimum-scale=x, maximum-scale=x"> where x is some magical number which I don't know how to find.
Any help would be appreciated.

this worked for me:
<script>
$(document).ready(function(){
var vpScale = window.outerWidth/800/window.devicePixelRatio;
var metas = document.getElementsByTagName('meta');
var i;
for (i=0; i< metas.length; i++) {
if (metas[i].name == "viewport") {
metas[i].content = "minimum-scale=" + vpScale + ", maximum-scale=" + vpScale;
}
}
});
</script>

Related

Flash Animate - Webview Scale/Zoomfactor

so i have a question about open websites with the webView.loadURL function. On ios the scale factor seems to be perfect, and the page fits perfect to the Rectangle I create to visualize the page. But if i create a build for andorid, the website opens with the maximum of zoom, so I have to tap into the screen and zoom out to see the complete page.
Has someone an idea how to determine the automatic zoom on android? It would be great if I load the page, and it would be showed completely, without a zoomfactor like on ios.
To change the size of the rectangle wouldn't be a solution, because I have a fix size, and I need to keep it.
Best regards and thanks a lot!
Hopefully it is not too late and hopefully this time it will be useful.
Try adding an element into the dom like this:
var webView:StageWebView = new StageWebView();
webView.viewPort = new Rectangle( 0, 0, this.stage.stageWidth, this .stage.stageHeight);
webView.stage = this.stage;
var htmlString:String = "<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>";
webView.loadString( htmlString );
If you can access the source code of the website, try to write this: <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />

Cordova InAppBrowser Doesn't scale the page loaded

Can anyone please help me get the external page that the Cordova InAppBrowser loads on Android app to fit to the phone screen size ?.
I am invoking the inapp browser from a Sencha Touch app using the follwing code.
var opt = 'location=no,clearcache=yes,closebuttoncaption=Back,EnableViewPortScale=yes';
window.open('http://sg-prod-mobilityapi.cloudapp.net','_blank', opt);
When ever I invoke the url I get the following screen
Could anyone please help me out ?.
Add this code into your index.html
<meta content=
"width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"
name="viewport" />
If the external page fit to the browser viewport, it should have a responsive design. I gone through that url, in that the middle div is set with fixed width and height of 340x380 px. you can only enable the zooming in in app browser.
Also phonegap allow you to execute some scripts after loading the url, just like injecting scripts.
var iabRef = window.open('http://sg-prod-mobilityapi.cloudapp.net', '_blank', 'location=yes');
iabRef.addEventListener('loadstop', replaceCustomStyle);
function replaceCustomStyle() {
iabRef.executeScript({
code: "var itm = document.querySelector('#Main div');
itm.setAttribute('style','width:100%');"
}, function() {
alert("Element Successfully Hijacked");
});
}
Try this and it is documented here

Compare viewport and screen size on mobile displays

As I've understood it, there are two supposedly reliable methods of determining the viewport width and the screen width on mobile displays. The viewport width is the virtual pixels, and screen width is the physical pixels. That's how it's supposed to be. But it's all the same for me. I've tested the following page with Android 4.0.4's stock browser and Chrome for Android in its current version (it won't tell me its number).
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="info"></div>
<script type="text/javascript">
setTimeout(function () {
var virtualWidth = document.documentElement.clientWidth;
var physicalWidth = screen.availWidth;
document.getElementById("info").innerHTML =
'Virtual width: ' + virtualWidth + '<br>' +
'Physical width: ' + physicalWidth + '<br>' +
'Ratio: ' + (physicalWidth / virtualWidth);
}, 500);
</script>
</body>
</html>
Here's the results:
The stock browser reports 540 for both values, Chrome reports 360 for both. The ratio is always 1. My phone is a Motorola RAZR i, and it should have a physical width of something around 540 (portrait orientation) and a ratio of 1.5. What's wrong with this page or the method, or the browsers? It's really hard to do mobile webdesign if the tools are totally unreliable. (And I'm not yet speaking of the random text size in Chrome...)
Update: Here's a live URL for you to do your own testing. Get a QR code of it.

Android viewport setting "user-scalable=no" breaks width / zoom level of viewport

I am working on a web app which has a width of 640px.
In the document head I set
<meta name="viewport" content = "width=640, user-scalable=no" />
so the content is nicely displayed and stretched horizontally.
This works perfectly on iOS but in Android the browser opens the website zoomed in so the user has to double click to zoom out and the entire page.
When I change the viewport setting to leave out the user-scalable tag like this:
<meta name="viewport" content="width=640" />
the Android browser adjusts nicely to the 640px - so it works.
The problem however now is, that users can zoom in and out on Android and iOS since the user-scalable tag is not set.
How can I forbid the scaling and at the same time set the viewport width to 640px on Android?
Trying rendering the viewport meta tag like so:
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
Setting scale settings will set user restrictions on how far they can zoom, and so if you set the initial and maximum to the same amount, this should fix the problem.
UPDATE: I was able to fix my bug for android devices all together by setting the below:
<meta name="viewport" content="width=640px, initial-scale=.5, maximum-scale=.5" />
I also noticed that some content, such as p tags were not flowing across the screen, so the hack for that would be to add the background-image property with empty string to any content that is stuck and is not going across the layout view. Hope this helps this time for you.
I wanted mobile to always show a website 640px wide because of a design that would break otherwise. (a design I did not make..) Thereby I wanted to disable zooming for mobile users. What worked for me me is the following:
- UPDATED 2013-10-31
First of all, there is no way you can do this without Javascript. You will have to check the user agent string. Therefore I created a mobile-viewport.js and included the script just before the closing tag:
function writeViewPort() {
var ua = navigator.userAgent;
var viewportChanged = false;
var scale = 0;
if (ua.indexOf("Android") >= 0 && ua.indexOf("AppleWebKit") >= 0) {
var webkitVersion = parseFloat(ua.slice(ua.indexOf("AppleWebKit") + 12));
// targets android browser, not chrome browser (http://jimbergman.net/webkit-version-in-android-version/)
if (webkitVersion < 535) {
viewportChanged = true;
scale = getScaleWithScreenwidth();
document.write('<meta name="viewport" content="width=640, initial-scale=' + scale + ', minimum-scale=' + scale + ', maximum-scale=' + scale + '" />');
}
}
if (ua.indexOf("Firefox") >= 0) {
viewportChanged = true;
scale = (getScaleWithScreenwidth() / 2);
document.write('<meta name="viewport" content="width=640, user-scalable=false, initial-scale=' + scale + '" />');
}
if (!viewportChanged) {
document.write('<meta name="viewport" content="width=640, user-scalable=false" />');
}
if (ua.indexOf("IEMobile") >= 0) {
document.write('<meta name="MobileOptimized" content="640" />');
}
document.write('<meta name="HandheldFriendly" content="true"/>');
}
function getScaleWithScreenwidth() {
var viewportWidth = 640;
var screenWidth = window.innerWidth;
return (screenWidth / viewportWidth);
}
writeViewPort();
The script checks if the visitor has an android (not chrome) or firefox browser. The android browser does not support the combination of width=640 and user-scalable=false, and the firefox browser does have a double screen width for some strange reason. If the visitor has a windows phone IE browser MobileOptimized is set.
I had the same situation, if you want the content to always fit the screen width without allowing the user to zoom in/out, use the following meta tags (this will work no matter what width you give)
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

Android how to sniff screen size from webkit browser?

The webserver wants to get info like screen size from each mobile handset that browses a webpage. The Javascript functions screen.width and screen.height return wildly inaccurate values.
Is there a way for the webserver to detect the screen size of the mobile handset? The client browser is webkit on Android.
You could try using CSS media queries, which should hopefully use the correct values.
<link rel="stylesheet" type="text/css"
media="only screen and (min-device-width: 320px)"
href="logResolutionScript?width=320" />
Use basically the same rules for other width and heights and a cookie to check if a client loads more than one stylesheet to get the correct value. You won't get the exact resolution, but it should be close enough. You can also check for orientation and use combinations of max-/min-(device)-width. Your file might end up with quite a lot of css-imports, but you should be able to pin down the resolution of the client quite accurately, unfortunately at the cost of a few HTTP-requests.
CSS3 Media Queries (Specification)
I found this article useful which mentions a meta tag that affects android and iPhone browsers which did what I needed:
<meta name="viewport" content="initial-scale=1.0">
From Mislav's article: What Mobile Safari does by default (i.e. without this directive) is display a zoomed-out, 980px-wide version of the page even if the layout itself is narrower. As content authors, with this directive we're saying "trust me, zoom to natural scale and I'll make sure it fits"
It seems to make the screen size and width values correct when read from JavaScript because the page is not zoomed (and if page is larger you can still scroll it). Alternatively there is probably a javascript variable to read out the zoom level.
For the iPhone this is documented here.
On Android you can work out the current zoom by adding an absolute div to the body of width 100%, and dividing the div's offsetWidth by window.innerWidth.
var iPadMeasureWidthNode = window.iPadWNode;
if (!iPadMeasureWidthNode) {
iPadMeasureWidthNode = window.iPadWNode = document.createElement('div');
// .ipad-measure-w {position:absolute; width:100%; top:-1px}
iPadMeasureWidthNode.className = 'ipad-measure-w';
document.body.insertBefore(iPadMeasureWidthNode, document.body.firstChild);
}
var zoominverse = 1000 / Math.round(1000 * iPadMeasureWidthNode.offsetWidth / window.innerWidth);
You can keep an element at 1:1 zoom by inverting (undoing) the amount of zoom:
// Not using scale3d because is hardware zooming which is ugly unreadable blurry pixel magnification.
node.style.webkitTransform = (zoominverse > 1) ? 'scale(' + zoominverse + ')' : '';
node.style.webkitTransformOrigin = (zoominverse > 1) ? '0 0 0' : '';
Zoom change is detected by window.onresize event (although resize event is delayed until after resize is completed... you can detect zoom start using the gesturestart event on iPad, or document.touchstart event and detect 2 fingers down).
Edit: After three corrections saying it doesn't work, I thought I better add an example showing it working. Tested works on: Android 4.1.2 normal browser, Android 4.1.2 Chrome, Android Opera Mobile 12.10, iPad 2 iOS4. (Didn't work on Android Firefox Mobile, and won't work in an iframe so jsfiddle won't work).
<!DOCTYPE html>
<html>
<head>
<style>
.ipad-measure-w {
position: absolute;
width: 100%;
top: -1px;
};
</style>
</head>
<body>
<button onclick="alertWidth()">alertWidth</button>
<div style="width: 1600px; height: 100px; background-color: blue;"></div>
<script>
function alertWidth() {
var iPadMeasureWidthNode = window.iPadWNode;
if (!iPadMeasureWidthNode) {
iPadMeasureWidthNode = window.iPadWNode = document.createElement('div');
iPadMeasureWidthNode.className = 'ipad-measure-w';
document.body.insertBefore(iPadMeasureWidthNode, document.body.firstChild);
}
var zoominverse = 1000 / Math.round(1000 * iPadMeasureWidthNode.offsetWidth / window.innerWidth);
alert(zoominverse);
}
</script>
</body>
</html>

Categories

Resources