Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get webview scale in Android 4

WebView.getScale() is deprecated (but still usable)

The recommended way to get scale for webview is to use WebViewClient.onScaleChanged() http://developer.android.com/reference/android/webkit/WebViewClient.html#onScaleChanged(android.webkit.WebView, float, float)

I've added corresponding handler in my custom webview:

public class CustomWebView extends WebView {

public CustomWebView(Context context) {
    super(context);
    setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale
        }
    });
}

And now the problem: the method not invoked (at least on my nexus 7) if I'm zoom in/zoom out using pinch, so it works good only if I use zoom controls button.

like image 222
Andriy Kopachevskyy Avatar asked Apr 18 '13 09:04

Andriy Kopachevskyy


1 Answers

I'm developing for API level 17 and I ended up using the deprecated method getScale() for the same reason. WebViewClient.onScaleChanged() simply wasn't triggered when I changed the scale using pinch zoom in the WebView. I have a custom GestureListener in my extended WebView that behaves differently when the view is zoomed in (scale 1.0+). I couldn't reliably get the proper scale value when I set a class variable in onScaleChanged().

like image 107
3k- Avatar answered Oct 20 '22 22:10

3k-