Can Android's WebView automatically resize huge images? Can Android's WebView automatically resize huge images? android android

Can Android's WebView automatically resize huge images?


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.


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>";}


You could use this:

WebView webView = (WebView) findViewById(R.id.myWebView);webView.getSettings().setLoadWithOverviewMode(true);webView.getSettings().setUseWideViewPort(true);

Found this solution here:How to set the initial zoom/width for a webview