Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the Android WebView print added margin?

We tries to print a webview content over google cloud print, but no matter what we do the resulted printout adds some margin.

Is there a way to remove this margin? We tried:

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

then

<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

then

mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

none worked...

like image 650
Guy Korland Avatar asked Sep 15 '16 18:09

Guy Korland


4 Answers

Use the following code to remove margins when printing the WebView.

@page{
margin-left: 0px;
margin-right: 0px;
margin-top: 0px;
margin-bottom: 0px;
}
like image 109
mihirjoshi Avatar answered Oct 14 '22 04:10

mihirjoshi


Just Use It *{margin:0px; padding:0px} Add In Your Style Sheet And Check Once

  *{margin:0px; padding:0px}
    body,html{padding:0px;margin:0px;}
 <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
like image 33
Samudrala Ramu Avatar answered Oct 14 '22 04:10

Samudrala Ramu


By Default HTML web pages have a padding and margin of 10px; You have to set in your head section or or css file:

<style type="text/css">
  html, body {
  width:100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
  }

It's work for me. Hope it will help you :)

or you can try another one:

Replace your tag with this one:

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

Here's another tip for images in a webview: add a styling that fits images in the width of the screen. Works great on all screen sizes:

<style type='text/css'>
      img {max-width: 100%;height:initial;} div,p,span,a {max-width: 100%;}
   </style>
like image 31
Jamil Hasnine Tamim Avatar answered Oct 14 '22 04:10

Jamil Hasnine Tamim


If using the css doesn't solve your issue, you can try using a TextView with fromHtml instead of using a webview:

TextView myTextView = (TextView) view.findViewById(R.id.my_textview);
Spanned textviewHtml;

//Note : fromHtml needs a display flag as second argument from API 24
if (Build.VERSION.SDK_INT >= 24) {
    textviewHtml= Html.fromHtml(yourHtmlHere, Html.FROM_HTML_MODE_COMPACT);
}
else {
    textviewHtml= Html.fromHtml(yourHtmlHere);
}

myTextView.setText(textviewHtml);

For more options on fromHtml you can refer to https://developer.android.com/reference/android/text/Html.html

Hope this helps! ;-)

like image 23
Yshanae Avatar answered Oct 14 '22 04:10

Yshanae