Android Enable Zoom and Pinch Controls in Webview

Viewed 3565

I'm trying to get zoom controls to display along with pinch to zoom to work in an android webview. Largely referencing this SO.

Right now I have these being set in the onCreate method:

this._webView.getSettings().setBuiltInZoomControls(true);
this._webView.getSettings().setDisplayZoomControls(true);
this._webView.getSettings().setSupportZoom(true);

But nothing appears and the Zoom controls don't appear. I've also tried without setSupportZoom but that didn't seem to help.

As far as I can tell my webview is not wrapped in a scroll view but it is a subclass of Webview:

class MyWebView extends WebView {
        public MyWebView(Context context) {
            super(context);
        }

        @Override
        public void goBack() {

            super.goBack();
        }

    }

So I end up creating it like this:

if (this._webView == null) {
            this._webView = new MyWebView(this);
            this._webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            this._webView.getSettings().setJavaScriptEnabled(true);
            this._webView.setWebChromeClient(new WebChromeClient());
            this._webView.setWebViewClient(new WebViewClient());
            this._webView.getSettings().setBuiltInZoomControls(true);
            this._webView.getSettings().setDisplayZoomControls(true);
            this._webView.getSettings().setSupportZoom(true);
            this._webView.loadUrl(this._baseUrl);
        }

Any ideas where I'm going wrong?

Thanks!

EDIT:

In onCreate I do this:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(1);
    getWindow().setFlags(1024, 1024);

    RelativeLayout my_layout = generateLayout();

    initUI();

    setContentView(my_layout);
}

In generateLayout we create the initial layout and create the holder for the webView:

private RelativeLayout generateLayout() {
    RelativeLayout my_layout = new RelativeLayout(this);
    my_layout.setId(this.generateViewId());
    my_layout.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT));

    ... more layout stuff

    FrameLayout webViewPlaceholder = new FrameLayout(this);
    this._webViewPlaceholder = webViewPlaceholder;
    webViewPlaceholder.setId(this.generateViewId());
    webViewPlaceholder.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
    RelativeLayout.LayoutParams webViewPlaceholderLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    webViewPlaceholderLayoutParams.addRule(RelativeLayout.BELOW, rlHeader.getId());
    webViewPlaceholder.setLayoutParams(webViewPlaceholderLayoutParams);

    my_layout.addView(webViewPlaceholder);
 }

Then in initUI we create the webView itself:

@SuppressLint({"SetJavaScriptEnabled"})
protected void initUI() {
    if (this._webView == null) {
        this._webView = new MyWebView(this);
        this._webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this._webView.getSettings().setJavaScriptEnabled(true);
        this._webView.setWebChromeClient(new WebChromeClient());
        this._webView.setWebViewClient(new WebViewClient());

        this._webView.getSettings().setSupportZoom(true);
        this._webView.getSettings().setBuiltInZoomControls(true);
        this._webView.getSettings().setDisplayZoomControls(true);

        this._webView.loadUrl(this._baseUrl);

    } else {
        if (this._webView.getProgress() >= 100) {
            this._progressIndicatorView.setVisibility(View.INVISIBLE);
        }
        setNavigationButtonsState();
    }
    this._webViewPlaceholder.addView(this._webView);
}
3 Answers
Related