7

when the user selects a share button within an activity, I would like to inflate a layout, populate it, and then write that layout to a pdf using the new printing API.

In my fragment I have

@TargetApi(Build.VERSION_CODES.KITKAT)
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_share_context:
        LayoutInflator inflator = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = inflator.inflate(R.layout.person_share, null, false);
        TextView name = (TextView)topContainer.findViewById(R.id.person_name);
        name.setText("Test Name");
        PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(612, 792, 1).create()
        PdfDocument.Page page = document.startPage(pageInfo)
        container.draw(page.getCanvas());
        document.finishPage(page)

        // write pdf
        ...

This unfortunately outputs a blank pdf document. However, when I use a view that already exists on the screen (inflated in onCreateView(..)), it shows up in the pdf as expected.

I'd appreciate any help.

James
  • 5,273
  • 10
  • 51
  • 76
  • Your layout has not gone through the layout and measure passes yet, and so my guess is that your `container` has a width and height of 0. – CommonsWare Jul 31 '14 at 10:34
  • @CommonsWare Thanks. Any idea of how to make it go through the layout and measure passes? Or can I set an explicit width and height? – James Jul 31 '14 at 10:38

1 Answers1

17

If my theory is correct -- that your inflated layout still has a width and height of zero -- you can call measure() and layout() to manually size and position things:

root.measure(800, 480);
root.layout(0, 0, 800, 480);

Given a View named root, this will have it (and its children, if any) fill an 800-pixel wide by 480-pixel high space (e.g., a WVGA device, landscape, full-screen). In your case, you should be able to call getWidth() and getHeight() on the Canvas to determine the sizes to use for measure() and layout().

FWIW, personally, I'd generate HTML and use that for the basis of printing, rather than a layout file. That's one of the techniques I demonstrate in this sample project.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Calling 'measure' and 'layout' worked. Thanks Comms. – James Jul 31 '14 at 22:25
  • Per your comment about generating HTML and using that to print, is it possible to create a PDF file from a webview and just save it without printing it? I couldn't figure out how to do that from your sample project. – James Aug 01 '14 at 00:12
  • @James: "is it possible to create a PDF file from a webview and just save it without printing it?" -- I don't believe so. Sorry, I thought your objective was to print, not just generate the PDF. – CommonsWare Aug 01 '14 at 00:14
  • Ok thanks anyway. Possibly http://stackoverflow.com/questions/20511639/how-to-create-pdf-from-webview-in-android – James Aug 01 '14 at 00:16
  • 1
    I don't know why no one has approved this answer or given it more up-votes. I was having the same exact problem and this fixed it for me. – miken.mkndev Jan 31 '15 at 13:53