4

There are hundreds of posts across SO and the rest of the internet about trying to fix viewport scaling in Android, and I'm now fairly certain that you just cannot set the initial-scale in a webview on Android 4.1.

I'm building a Cordova (Phonegap) app, and I've got every thing scaled nicely on the iPhone 3GS, 4S and 5 and an iPad 2, running iOS7 and the 3GS on iOS6

I've also got the UI scaled to fit on the Moto G, LG Nexus 5, Google Nexus 5 and Samsung Galaxy S4, all running Android 4.4

But on a Samsung Galaxy S2 and S3 Mini running Android 4.1, I cannot set the initial-scale.

The HTML viewport meta tag doesn't work on 4.1 or 4.4 (works in Chrome, not in the WebView or default browser):

<meta name="viewport" content="user-scalable=no, initial-scale=0.5, width=device-width, height=device-height, target-densitydpi=device-dpi" />

I've knocked up an absolutely basic Cordova project from the cli, cordova create basicProject, and if I add some Java to the onCreate method in the main Activity, specifically the setInitialScale method:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
    // Set by <content src="index.html" /> in config.xml
    super.loadUrl(Config.getStartUrl());
    //super.loadUrl("file:///android_asset/www/index.html");

    this.appView.setInitialScale( 50  );

}

Then I get the Moto G on 4.4 scaling as expected:

Moto G on Android 4.4 with an initial-scale of 0.5, via setInitialScale(50)

But exactly the same project running on the Galaxy S2, does not get scaled.

Galaxy S2 on Android 4.1 with an initial-scale of 0.5, via setInitialScale(50)

I'm limited to testing on Samsung devices with 4.1, or a range of devices on 4.4, so if anyone can test the same thing on 4.2 or 4.3, or anyone on 4.1 not with a Samsung that'd be helpful.

Does anyone know how to get Android 4.1 to obey setInitialScale()

TylerH
  • 20,799
  • 66
  • 75
  • 101
Pete
  • 4,542
  • 9
  • 43
  • 76

2 Answers2

8

You should be able to add some javascript to your webpages that will scale the content when picked up by your webview. Here is an example that will scale your html content so that the content fits to the available width of the screen:

function customScaleThisScreen() 
    var contentWidth = document.body.scrollWidth, 
        windowWidth = window.innerWidth, 
        newScale = windowWidth / contentWidth;
    document.body.style.zoom = newScale;
}

This will work on older (pre 4.2) and newer chromium-based (4.2+) webviews.

TylerH
  • 20,799
  • 66
  • 75
  • 101
petey
  • 16,914
  • 6
  • 65
  • 97
  • 1
    I actually have a slider in the view, which puts the content far wider than I'd like to scale it by - but a quick modification to use the device pixel ratio and some device detection code works, `document.body.style.zoom = 1 / window.devicePixelRatio` – Pete Jul 16 '14 at 06:48
1

@Petey showed me the solution but for completeness here's my version with a little more phonegap context:

var app = {
    initialize: function() {
        this.bindEvents();
    },
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function() {

        // PETEY'S SOLUTION, WRAPPED IN CODE TO CHECK FOR ANDROID PRE 4.2
        // ALSO MODIFIED TO USE DEVICE PIXEL RATIO, INSTEAD OF CONTENTWIDTH
        if( isAndroid() ) {
            var matches = device.version.match( /[0-9]+(\.[0-9]+)?/i );

            if( matches.length && parseFloat( matches[ 0 ] ) < 4.2 ) {
                document.body.style.zoom = 1 / window.devicePixelRatio;
            }
        }

        // start app logic

    }
};

app.initialize();

function isAndroid() {
    if( device.platform.match( /android/i ) ) {    
        return true;
    }

    return false;
}
Pete
  • 4,542
  • 9
  • 43
  • 76