Home > OS >  How can i display the current reading page in a pdf App using Android studio
How can i display the current reading page in a pdf App using Android studio

Time:03-23

I am currently developing a pdf reader app and I want the app to display the current reading page Incase the App is closed and reopened again. I'm using this dependency implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

CodePudding user response:

Here is Kotlin's help, Save your page number in SharedPreferences in onDestroy(). When you open your app Then get the page number from SharedPreferences and load pdf.

MainActivity.kt

class MainActivity : AppCompatActivity() {
private var defaultPage = 0
 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        defaultPage = ... // get value form SharedPreferences
        loadPdfViewer()

    }

private fun loadPdfViewer() {
        pdfView.fromFile(pdfPath)
            .enableSwipe(true) // allows to block changing pages using swipe
            .swipeHorizontal(false)
            .enableDoubletap(true)
            .defaultPage(defaultPage)
            .enableAntialiasing(true) // improve rendering a little bit on low-res screens
            // spacing between pages in dp. To define spacing color, set view background
            .spacing(5)
            .onError {
                // do something
            }
            .autoSpacing(false) // add dynamic spacing to fit each page on its own on the screen
            .pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view
            .nightMode(false) // toggle night mode
            .load()

    }

   override fun onDestroy() {
        super.onDestroy()
        defaultPage = pdfView.currentPage
        // save this defaultPage number in SharedPreferences
    }
}

CodePudding user response:

You can do this with simple logic.

Assuming this is an offline or online application. Each time you load a pdf page, you can get the page number each time the user opens a new page or goes back to previous page. You can store this page when the onDestroy() or the 'onStop()' method is called.

You can store the page information using:

  1. SharedPreferences.
  2. Simple SQLite database.
  3. Remote database, if the application has remote/online backend.

Each time the onResume() method is called, you can fetch the last page information and pass it to the pdf loader or WebView and it will load the page number. In case the page information cannot be retrieved, you can then load the first page of the pdf.

  • Related