0

I want to show a PDF in a custom Dialog. I fetch PDF from a service as a ByteArray. I pass the ByteArray to PDFView object and then inflate the Dialog. All the views in the dialog are visible except the pdfview.

Here is my activity code:

val fileName = "filename.pdf"

val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.activity_pdf_view)
val pdfView = dialog.findViewById(R.id.pdfView) as PDFView
val closeBtn = dialog.findViewById(R.id.closeBtn) as ImageButton
val downloadBtn = dialog.findViewById(R.id.downloadBtn) as Button

pdfView.fromBytes(byteArray).load()

dialog.show()

Below is the xml activity_pdf_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/ticketLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@drawable/bg_ticket_layout">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Ticket"
            android:textSize="15sp"
            android:textColor="@color/black"
            android:layout_margin="25dp"/>

        <com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintTop_toBottomOf="@id/title"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginTop="39dp"
            android:layout_marginBottom="8dp"
            android:layout_marginHorizontal="8dp"/>

        <ImageButton
            android:id="@+id/closeBtn"
            android:layout_width="11dp"
            android:layout_height="11dp"
            android:background="@color/white"
            android:contentDescription="@string/close"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_cancel_icon"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="24dp"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/downloadBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/submit_btn_bg"
        android:drawableStart="@drawable/ic_file_download_white"
        android:text="@string/download_pdf"
        android:textColor="@color/white"
        android:textSize="15sp"
        app:layout_constraintEnd_toEndOf="@id/ticketLayout"
        app:layout_constraintStart_toStartOf="@id/ticketLayout"
        app:layout_constraintTop_toBottomOf="@id/ticketLayout"
        android:paddingHorizontal="19dp"
        android:drawablePadding="10dp"
        android:layout_marginTop="15dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

This is how the view looks:

enter image description here

I debugged the code and confirmed that byteArray is not empty. All the other views like the close button and download button function properly as expected. Only the pdfview doesn't show anything.

I had tried many things first like AlertDialog.Builder, an activity with Dialog theme and many more ways and finally broke the code down to the simplest possible way. Still no change in view.

Please help me find out the issue here. Thanks a lot in advance :)

Update:

The issue is that if I use wrap_content for parent ConstraintLayout, it shows this small view as in screenshot. But when I use match_parent, it shows PDFView correctly (but stretched throughout the screen that looks ugly though). So what I did is hardcoded the height of parent constraint layout. I did some digging on the internet and found that the PDF inside the PDFView is not loaded when the parent ConstraintLayout draws its child layouts. So does anyone know any way where I can redraw the layout after all data is loaded?

Rahul
  • 143
  • 1
  • 17

1 Answers1

0

As we all know Android Deprecated to show PDF via webview here you can find useful link. https://github.com/eduxcellence/PDFViewer/find/master

akash kumar
  • 1
  • 1
  • 1