3

I have a custom PrintDocumentAdapter and draw pages on my own, so I should draw xml layout on the page canvas:

private void drawPage(PdfDocument.Page page, List<Object> objects,
                          int pageNumber) {
   Canvas canvas = page.getCanvas();
   ...
}

there is an TableLayout 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 wrap_content and match_parent but none of them worked. I even tried fix width and height.

private void drawPage(PdfDocument.Page page, List<Payment> 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< objects.size(); ++i){
//... some code
        }

        frameLayout.addView(v);
        frameLayout.measure(200 , 200);
        frameLayout.layout(100, 100, 100, 100);
        frameLayout.draw(canvas);
        canvas.restore();
    } 

R.layout.payments_table_layout:

<?xml version="1.0" encoding="utf-8"?>
<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">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="some text"
            style="@style/CustomFont.Wave"/>
    </FrameLayout>
    <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">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#ccc">
            <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"
                />
            <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"/>
            <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"/>
            <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"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

Now the Result:
results

Mneckoee
  • 2,802
  • 6
  • 23
  • 34

2 Answers2

1

I shouldn't stretch all columns of TableLayout so I changed this attribute to :

android:stretchColumns="1,3,7"  
android:shrinkColumns="5"

and set the layout width to width of the page like this ( we should care the dimension unit): in my customPrintDocumentAdapter

@Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
        myPdfDocument = new PrintedPdfDocument(context, newAttributes);

        pageHeight =
                newAttributes.getMediaSize().getHeightMils()/1000 * 72;
        pageWidth =
                newAttributes.getMediaSize().getWidthMils()/1000 * 72;

        ...
    }

and my new drawPage method:

private void drawPage(PdfDocument.Page page, List<Payment> payments,
                          int pageNumber) {
... 
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(pageWidth, pageHeight));
...
}
Mneckoee
  • 2,802
  • 6
  • 23
  • 34
0

please try below xml , in which I used weightsum and layout weight in TableRow and its child textviews

android:weightSum="4"

android:layout_width="0dp"
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
    <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">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="some text"
               />
        </FrameLayout>
        <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">
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="4"
                android:layout_gravity="center_horizontal"
                android:background="#ccc">
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"
                    />
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>
Community
  • 1
  • 1
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • thanks but I don't want same width for cells. cells have different content so their width should be different – Mneckoee Jan 23 '17 at 12:49