4

I am using android webview in my application.I noticed that it has the inbuilt margin.So need to remove it as per UI requirement.

I have used this it's getting the perfect result.

 @Override
        public void onPageFinished(WebView view, String url) {
//            super.onPageFinished(view, url);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("javascript:document.body.style.margin=\"0%\"; void 0");
            webView.setVisibility(View.VISIBLE);
        }

it's getting refreshed when it's notified and it's looks as too bad. But we can apply the JavaScript to our html code as well right? I have used <script> <script/> over there but no result.

webView.loadDataWithBaseURL(null, "<style>a{color:#3D7B8A; text-decoration:none}</style>"+ getDescription().replace("<p>", "<p style= \"line-height: 150%; text-align: justify;\" >"), "text/html", "utf-8",
                    null);

Please advise.

Smit Patel
  • 1,174
  • 3
  • 26
  • 40
  • When you load a javascript URL, it runs javascript which can modify the DOM of the document already loaded. But if you don't have javascript: URL, it just thinks it's a new web page and replaces the already loaded document. – kris larson Dec 16 '15 at 09:59
  • @krislarson right now the margin is removing from the webview but can we put javascript with the loadDataWithBaseURL? – Smit Patel Dec 16 '15 at 10:35
  • You could try it with a base URL of "javascript:" and a mime type of "application/javascript", but I've never tried it so I don't know if it works. I use `loadUrl` all the time for javascript because I know it works. – kris larson Dec 16 '15 at 10:47
  • 1
    Have you tried removed all the margins by injecting in the `` a style reset `* {margin: 0; padding: 0}`? – avetisk Dec 16 '15 at 13:00
  • @avetisk yes i did but it remove the padding between line as well. And i just need to remove the outline margin – Smit Patel Dec 16 '15 at 13:51
  • If it works, then try `html, body, body > * { padding: 0; margin: 0 }` – avetisk Dec 16 '15 at 14:02

1 Answers1

3

As far as I'm aware, from the code above, you haven't made padding = 0.

I have look on Google and found this question and answer, you may want to have a look: Remove unwanted White Space in WebView Android

By default webview has some margin/padding in body. If you want to remove that padding/margin then override body tag and add margin like:

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

Community
  • 1
  • 1
Josh Murray
  • 619
  • 5
  • 18