8

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.

Andriy Kopachevskyy
  • 7,276
  • 10
  • 47
  • 56
  • Did you find the answer by now? I've got the same problem. – Bart Burg Jun 05 '13 at 14:44
  • Didn't find way to catch scale changed even, but my aim was to track current scale, so I was forced to use deprecated WebView.getScale() instead scale listener approach. If you do need to track scale change event on webview try to set custom gesture detector to webview and simply track pinch events. Don't sure it will works, just idea. – Andriy Kopachevskyy Jun 06 '13 at 16:18
  • I don't have a solution yet (and I don't think there's a direct one given the following), but I've discovered the reason: the `ScaleDetectorListener.onScaleEnd` callback in `WebView` (and later the `ZoomManager` helper class) results in either `setNewZoomScale` (for the `WebKit` version) or `setZoomScale` (for the `ZoomManager` version) being called with scale `mActualScale`, and those methods will only trigger `onScaleChanged` when the supplied scale does not equal `mActualScale` (there is another `!mPreviewZoomOnly` requirement, but that one is satisfied as `onScaleEnd` sets it to `false`). – JAB Mar 26 '14 at 14:15
  • The annoying thing is that this bug has been around since at least Gingerbread, but I see nothing regarding it in the Android issue tracker and with the shift to a Chromium-based implementation in KitKat it's likely the issue will never be resolved for classic WebView. – JAB Mar 26 '14 at 14:25

2 Answers2

3

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().

3k-
  • 2,467
  • 2
  • 23
  • 24
1

try :

public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    setWebViewClient(new WebViewClient() {
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            super.onScaleChanged(view, oldScale, newScale);
            currentScale = newScale;
        }
    });
}
smr fakhri
  • 27
  • 1