Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView.zoomIn() not working before rendering view

I am calling wv.zoomIn() in my fragment's onCreateView, and it is having no effect.

Full story ...

My fragment contains a slider which I use to programmatically zoomIn/zoomOut a webview. This is working fine.

I now want to store the last used zoom level, and apply it to the webview next time it is displayed. So in the onCreateView I am retrieving the zoom level and calling zoomIn an appropriate number of times.

public View onCreateView(LayoutInflater inflater...
...
if (savedZoom > defaultZoom) { 
 for (int i=defaultZoom; i< savedZoom; i++) {
   MyLog.d("zooming in");   // appears x times in log as expected
   boolean zoominResult = wv.zoomIn();
   MyLog.d("zoominResult = "+zoominResult); // shows zoomIn returns FALSE
 }
}

The logs confirm that wv.zoomIn() is being called the correct number of times, but the displayed webview is still at the default zoom.

Any suggestions such as a way to call the zoomIn() post-render, or an alternative way to preserve and restore the zoom settings of the webview?

like image 768
pinoyyid Avatar asked Oct 01 '22 09:10

pinoyyid


1 Answers

I think this code will help:

webView.clearView();
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("< your URL >");

int scale = (int) (100 * webView.getScale());

webView.setInitialScale(scale);

webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setBuiltInZoomControls(true);
like image 119
Darshak Avatar answered Oct 05 '22 10:10

Darshak