title: how to print a layout with Print Document Adapter in android
body: <p>i  want to preview a layout for printer and want to print that layout. i already searched a lot but didn't  find any way. i dont want to print many pages of document i just want to print one page one layout.
  the layout contains text information, one list view and one imageView of QR code.</p>
  
  <p>I have tried the following code: </p>
  
  <pre><code> private void doPrint() {
  
  
          // Get a PrintManager instance
          PrintManager printManager = (PrintManager) getApplicationContext()
                  .getSystemService(Context.PRINT_SERVICE);
  
          // Set job name, which will be displayed in the print queue
          String jobName = getApplicationContext().getString(R.string.app_name) + " Document";
  
          // Start a print job, passing in a PrintDocumentAdapter implementation
          // to handle the generation of a print document
          printManager.print(jobName, new MyPrintDocumentAdapter(getApplicationContext()),
                  null);
  
  
      }
  </code></pre>
  
  <p>and this is my Adapter class </p>
  
  <pre><code>package com.example.haier.arksolsfollow;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.os.CancellationSignal;
  import android.os.ParcelFileDescriptor;
  import android.print.PageRange;
  import android.print.PrintAttributes;
  import android.print.PrintDocumentAdapter;
  import android.widget.Toast;
  
  class MyPrintDocumentAdapter extends PrintDocumentAdapter {
      public MyPrintDocumentAdapter(Context applicationContext) {
      }
  
      @Override
      public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
          computePageCount(oldAttributes);
      }
  
      @Override
      public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
  
      }
  
      @Override
      public void onStart() {
          super.onStart();
  
      }
  
      @Override
      public void onFinish() {
          super.onFinish();
      }
  
      private int computePageCount(PrintAttributes printAttributes) {
          int itemsPerPage = 4; // default item count for portrait mode
           // default item count for portrait mode
  
          PrintAttributes.MediaSize pageSize = printAttributes.getMediaSize();
          if (!pageSize.isPortrait()) {
              // Six items per page in landscape orientation
              itemsPerPage = 6;
          }
  
          // Determine number of print items
          int printItemCount = 1;
  
          return (int) Math.ceil(printItemCount / itemsPerPage);
      }
  }
  </code></pre>
  
  <p>my app also get crashes in this line </p>
  
  <pre><code>printManager.print(jobName, new MyPrintDocumentAdapter(getApplicationContext()),
                  null);
  </code></pre>
  
  <p>kindly guide me how to use all call back methods in adapter and how to  preview a layout for printer </p>