views:

675

answers:

5

I have a WebView which I'm trying to have scale to a certain percent on loading the page. The iPhone version of this software uses the HTML meta-tag:

<meta name="viewport" content="width=320, initial-scale=0.95, maximum-scale=2.0, user-scalable=1">

Since Android's WebView doesn't seem to respect that tag I hard-coded the percent using setInitialScale(). However, the WebView is just flat-out ignoring this method call. No matter what number I put in there it shows at 100%.

Ideas?

Update: It's not working in the emulator, on my Droid (Motorola), or on my G1 (HTC).

A: 

did you enable the zoom support for your webview?

    WebSettings webSettings = mywebView.getSettings();
    webSettings.setSupportZoom(true);       
Jorgesys
I hadn't, but I just tried it and it made no difference.
fiXedd
A: 

Some HTC devices have problems with WebView, try this snippet!

import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

myWebView = new WebView(this);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setInitialScale(100);      
try {
   Method m = myWebView.getClass().getMethod("setIsCacheDrawBitmap", boolean.class);
    if (m != null) {
        m.invoke(myWebView, false);
        myWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
    }
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}

myWebView.loadUrl("http://www.elnorte.com");
Jorgesys
I found it this is the original link http://community.htc.com/na/htc-forums/android/f/91/p/2332/8538.aspx =)
Jorgesys
Made no difference... thanks though.
fiXedd
A: 

What version are you running? It seems that 2.0 and below had an issue with this but was resolved in the 2.1.

Ryan Hayes
A: 

Can see several options.

1 . Probably there's something wrong with your code. Here's the most simple code and I can tell it works for me. Content is scaled.

WebView webview1 = (WebView) findViewById(R.id.webview1);
webview1.setInitialScale(30);
webview1.loadUrl("http://stackoverflow.com");

So please post your code. All code related to WebView configuration. I test on emulator 2.2

2 . Probably there's something wrong with url you test. You can test "http://stackoverflow.com" as I test with it and it is scaled fine.

3 . Maybe we use different SDKs. What SDK do you build against? I mean what is your target in default.properties? What is your uses-sdk in AndroidManifest.xml?

Here's a simple test that works fine for me http://open-pim.com/tmp/WebViewTest.zip. You may try it too.

Fedor
This worked! But not in my apps. Turns out the meta viewport tags: initial-scale=1.0; maximum-scale=1.0; were preventing setInitialScale() from working. After removing them it works.
morganchristiansson
A: 

The meta tag works for me, if I separate the parameters with semicolons (;) instead of commas (,). I've used this in the past:

<meta name="viewport" content="initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />

And it works, at least in the browser. I'm not sure about a WebView, but it might be worth a shot.

Felix