Learning Android Development

Displaying PDF in ViewPager2

Without using external PDF viewer App

Photo by Dmitry Ratushny on Unsplash

The most convenient way for your App to read a provided PDF is to pass it through an intent to an external App as below

Intent(Intent.ACTION_VIEW).apply {
setDataAndType(pdfPathUri, "application/pdf")
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
startActivity(this)
}

However, that’s limiting because

  • That will assume there’s a PDF reader app in the phone
  • The user will go out of your app, and might not get back
  • You can’t get to read PDF file in your assets

So the better way is to show the PDF within your App instead.

Display PDF in ViewPager2

Let’s load the PDF in our App instead, ane let’s use the latest and shiniest new ViewPager2 for it in Kotlin as below.

Here I show you how to extract the PDF file and view it step by step.

It is just simply 2 simple steps as shown above.

1. Convert File to…

--

--