I WebView data in WebView and expect that the picture as the text will occupy the entire width of the screen. But on the sides, that the text, that the picture remains free space of approximately 8dp .

In the activit class:

 // Set content String content = bodyContent; //сюда передаю тело документа с разметкой. Display display = getWindowManager().getDefaultDisplay(); int displayWidth = display.getWidth(); View webLayout = getLayoutInflater().inflate(R.layout.partial_article_web, null); web = webLayout.findViewById(R.id.content); WebSettings settings = web.getSettings(); settings.setTextZoom(storage.getTextZoom()); settings.setSupportZoom(false); settings.setJavaScriptEnabled(true); settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); web.addJavascriptInterface(this, "android"); web.loadDataWithBaseURL("", asHtmlPage(content, "text/html", "utf-8", null); 

And here is how I form the data for loading in WebView :

 private String asHtmlPage(String body) { String style = "<style>" + "body { font-family: 'sans-serif-light', sans-serif; }" + "img { display: inline; height: auto; max-width: 100%; } " + "</style>"; String meta = "<meta name='viewport' content='width=device-width, initial-scale=1'>"; return "<html><head>" + style + meta + "</head><body>" + body + "</body></html>"; } 

WebView Markup:

 <WebView android:id="@+id/content" style="?android:textAppearanceMedium" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="16dp" android:paddingBottom="16dp" android:scrollbars="none" /> 

I tried:

  1. comment

     settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true); 
  2. moved the meta block with <head> in <body>
  3. Changed: <meta name='viewport' content='width=" + displayWidth + " , initial-scale=1'>";

  4. In style changed max-width: 100%; on max-width: " + displayWidth + ";

The content that I load does not have any indents or borders on the sides.

Help me figure out where this unexpected Padding comes from.

UPD: As noted by @Timur Mukhortov, corrected @dimen/activity_horizontal_margin in the xml markup of WebView at 16dp .

  • @Timur Mukhortov thanks for the tip. Corrected. - V.March
  • so, it is better if you have them set everywhere as standard. Well, the type can create a separate indented resource for WebView and use it everywhere. - Timur Mukhortov 4:21 pm
  • I changed them and more obvious figures only in the question. And so, I try to use links. - V.March

1 answer 1

In your content variable, add the following container:

 <body style="margin: 0; padding: 0"> 

By default, webview has some indent / indent in the body. If you want to remove this indent / field, override the body tag ..

  • Sorry, misleading "@ dimen / activity_horizontal_margin" is 16dp. But in this case it is only a link to the resource. And it is applied to the top and bottom fields. And my side removed. - V.March
  • try now - Timur Mukhortov February
  • It worked! Thank you very much! - V.March
  • I'm glad that you helped) - Timur Mukhortov February