android – How do I adjust the appearance of the webview when displaying images and text?-ThrowExceptions

Exception or error:

when i display an image on webview then the text becomes smaller, so how do you make it look appropriate?

this is the appearance!

enter image description here

I want to make it look neater like this. or shrink the image according to the layout

enter image description here

How to solve:

To change web setting :

WebSettings setting = web.getSettings();
Resources res = getResources();
fontSize = res.getDimension(R.dimen.sp_20);
setting.setDefaultFontSize((int)fontSize);
setting.setRenderPriority(RenderPriority.HIGH);
setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
setting.setAppCacheEnabled(false);
setting.setBlockNetworkImage(true);
setting.setLoadsImagesAutomatically(true);
setting.setGeolocationEnabled(false);
setting.setNeedInitialFocus(false);
setting.setSaveFormData(false);

###

You can apply custom css to the webview content and load it.
Example code below which you can wrap in a method and call it on onPageStarted or onPageFinished callback while setting webviewclient for the webview.

Example :

            InputStream inputStream = context.getResources().getAssets().open("my_css.css"); //Your css file stored in assets
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            inputStream.close();
            String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
            webview.loadUrl("javascript:(function() {" +
                    "var parent = document.getElementsByTagName('head').item(0);" +
                    "var style = document.createElement('style');" +
                    "style.type = 'text/css';" +
                    "style.innerHTML = window.atob('" + encoded + "');" +
                    "parent.appendChild(style)" +
                    "})()");
        } catch (Exception e) {
            //Exception
        }

Leave a Reply

Your email address will not be published. Required fields are marked *