title: Android: draw xml Layout on canvas
body: <p>I have a custom <code>PrintDocumentAdapter</code> and draw pages on my own, so I should draw xml layout on the page canvas:  </p>
  
  <pre><code>private void drawPage(PdfDocument.Page page, List&lt;Object&gt; objects,
                            int pageNumber) {
     Canvas canvas = page.getCanvas();
     ...
  }
  </code></pre>
  
  <p>there is an <code>TableLayout</code> in my xml. I want table width to fill the entire page but it exceed the page if the width bigger than page width or is smaller than the page if its width is smaller than the page width. I used both <code>wrap_content</code> and <code>match_parent</code> but none of them worked. I even tried fix width and height.  </p>
  
  <pre><code>private void drawPage(PdfDocument.Page page, List&lt;Payment&gt; payments,
                            int pageNumber) {
          Canvas canvas = page.getCanvas();
          PdfDocument.PageInfo pageInfo = page.getInfo();
  
          canvas.save();
          canvas.translate(leftMargin , topMargin);
  
          FrameLayout frameLayout = new FrameLayout(context);
          frameLayout.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
          LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View v = li.inflate(R.layout.payments_table_layout, null);
          v.setLayoutParams(new   
  FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  
          TableLayout tableLayout = (TableLayout) v.findViewById(R.id.payments_table_layout);
  
          for(int i= 0 ; i&lt; objects.size(); ++i){
  //... some code
          }
  
          frameLayout.addView(v);
          frameLayout.measure(200 , 200);
          frameLayout.layout(100, 100, 100, 100);
          frameLayout.draw(canvas);
          canvas.restore();
      } 
  </code></pre>
  
  <p>R.layout.payments_table_layout:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center"&gt;
      &lt;FrameLayout
          android:layout_width="match_parent"
          android:layout_height="50dp"&gt;
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:text="some text"
              style="@style/CustomFont.Wave"/&gt;
      &lt;/FrameLayout&gt;
      &lt;TableLayout
          android:id="@+id/payments_table_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_gravity="center_horizontal"
          android:stretchColumns="*"
          android:layout_weight="1"&gt;
          &lt;TableRow
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:background="#ccc"&gt;
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="some text"
                  android:layout_gravity="center"
                  android:background="@drawable/table_border"
                  android:padding="4dp"
                  /&gt;
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="some text"
                  android:layout_gravity="center"
                  android:background="@drawable/table_border"
                  android:padding="4dp"/&gt;
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="some text"
                  android:layout_gravity="center"
                  android:background="@drawable/table_border"
                  android:padding="4dp"/&gt;
              &lt;TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:text="some text"
                  android:layout_gravity="center"
                  android:background="@drawable/table_border"
                  android:padding="4dp"/&gt;
          &lt;/TableRow&gt;
      &lt;/TableLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Now the Result:<br>
  <a href="https://i.stack.imgur.com/hBBii.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hBBii.png" alt="results"></a></p>