Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android's WebView automatically resize huge images?

In some web browsers, huge images are automatically resized to fit the screen.

Is it possible to do the same in an Android WebView?

The web page just contains the image, maybe adding some JavaScript could do the trick?
Anybody has already done this?

Note: I don't know the size of the image in advance.

like image 597
Nicolas Raoul Avatar asked Jun 23 '10 06:06

Nicolas Raoul


2 Answers

Yes, it's possible. You can try setting the WebView Layout using the code below. It resizes all Images (Greater than the Device Screen Width) to the Screen Width. This works for both Orientations (Portrait and Landscape)

webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 

You can add extra margins/padding later to get the spacing right.

like image 97
Sheharyar Avatar answered Sep 21 '22 18:09

Sheharyar


webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 

works but is deprecated. This is another solution without LayoutAlgorithm.SINGLE_COLUMN, using CSS:

@SuppressLint("NewApi") private openWebView() {     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {         webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);     } else {         webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);     }     String data = "<div> your HTML content </div>";     webView.loadDataWithBaseURL("file:///android_asset/", getHtmlData(data), "text/html", "utf-8", null); }  private String getHtmlData(String bodyHTML) {     String head = "<head><style>img{max-width: 100%; width:auto; height: auto;}</style></head>";     return "<html>" + head + "<body>" + bodyHTML + "</body></html>"; } 
like image 26
Yair Kukielka Avatar answered Sep 24 '22 18:09

Yair Kukielka