WebView Text Size

Sometimes, when I load my page (static content created on the fly), I see that the font size is too small 1 . If I reboot, I see it correctly 2 . I go back and forth and see it right. And then ... small. Not a specific page, not at a specific time. Not even a specific version: I deploy ICS on the device, without problems, and then change something (for example, font size), and that is the problem. The same goes for deployments on 8 and 10 emulators.

My view is pretty simple:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" > <WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webviewArticle" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:padding="12dp" > </WebView> </LinearLayout> 

HTML construct (content parameter formatted by html)

 String toShow = new StringBuffer() .append("<html>") .append("<head>") .append("<style type='text/css'>body{ font-size: large; color: #FFFFFF; background-color: #000000;}</style>") .append("</head>") .append(String.format( "<body><h1>%s</h1><div><img src='%s' alt='%s' style='width:100%%; height:auto' /></div><div style='background-color:#000000;'>%s</div><div><h3>%s</h3></div></body>", article.getTitle(), article.getImageLink().toString(), article.getTitle(), article.getContent().replace("%", "%25"), //html article.getAuthor())) .append("</html>") .toString(); mBrowser.getSettings().setBuiltInZoomControls(true); mBrowser.setInitialScale(1); CompatibilityUtils.setLoadWithOverviewMode(mBrowser, true); //set based on android version mBrowser.getSettings().setUseWideViewPort(true); mBrowser.loadDataWithBaseURL("fake://in/order/to/refresh", toShow, "text/html", "utf-8",null); 

Any clues?

Additional information 12/09/25: as the images show, WebView considers that the available space takes up half the screen and accordingly places the text. This is strange. The strangest thing is that he thinks so for the text (the title above the image and the div below), not the image !!!

+2
source share
1 answer
 WebSettings settings= webView.getSettings(); 

this is

 settings.setTextSize(WebSettings.TextSize.SMALLEST); 

or

 settings.setDefaultFontSize(10);//Larger number means larger font size 
+7
source

Source: https://habr.com/ru/post/972906/


All Articles