question

Jeffrey Blattman avatar image
Jeffrey Blattman asked Jeffrey Blattman commented

How do I use ViewPrintJob? I get "Measured view width or height is 0"

When I pass my view into ViewPrintJob and call print, I get an exception saying that "Measured view width or height is 0"?
Print
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Jeffrey Blattman avatar image
Jeffrey Blattman answered Jeffrey Blattman commented
Before a view can be printed it must be laid out. This is the process of providing the layout dimensions and letting Android fit the various view components into this space. Normally this process does not happen until the view is added to another layout that is attached to an activity (or fragment). To use ViewPrintJob we must force the issue. Here's a helper method to do that:

public void layoutAndMeasureView(View view, int viewWidth) {
    int measuredWidth = View.MeasureSpec.makeMeasureSpec(viewWidth, View.MeasureSpec.EXACTLY);    
    int measuredHeight = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);    
    view.measure(measuredWidth, measuredHeight);    
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());    
    view.requestLayout();  
}
The viewWidth parameter should be the width of the printer. For Clover's supported printers that's either 576 or 384 (pixels). Knowing which to use requires you to know to which printer your print job will target. If you plan on providing an explicit printer when you call print() on your print job (not typical), you can first look up the TypeDetails for that printer with PrinterConnector.getTypeDetails(), and inspect the resulting TypeDetails.getNumDotsWidth().

Otherwise, choose the larger width (576). If printed on a smaller width printer the contents will be scaled.

Look for an updated version of ViewPrintJob that will have the above layout and measure step built in. Use it by calling a new builder method view(View, width).
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

kirank avatar image kirank commented ·

@Jeffrey Blattman - We are little late to comment. But we were hitting this in latest SDK even with layoutAndMeasureView inside the SDK. This is because view may not be completely laid out and ready for measurement. This was common if WebView is used. Simple native Android layouts also throw this, but if we wait for the rendering to be done, success rate is much better.

0 Likes 0 ·
Jeffrey Blattman avatar image Jeffrey Blattman ♦♦ kirank commented ·

I don't see anywhere in our code where we wait for layout and measure to complete or otherwise give it extra time. It's *possible* that the timing is just such that gets enough time to do it's work before we try to turn it into a bitmap, but seem unlikely. Seems like an easy theory to test.

Sure, for a web view you'd need to wait for the page to load before trying to print the view. There are callbacks for that on the web view client listener. I haven't tested the specific of it though.

For testing you can also turn the view into a bitmap yourself, then print the bitmap instead of the view (ImagePrintJob). That way you can write the bitmap to a PNG and inspect it just to see exactly what you are sending to the printer.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community