Android Studio App open PDF file with a built-in PDF viewer

Android Studio 
Android App open PDF file with a built-in PDF viewer

  • Create a new project "Empty Activity" Language "Java" Minimum API 25
1. Create an assets folder and copy the pdf file into it.

Right on the app > New > Folder > Assets Folder

2. replace all codes in build.gradle (Module: app)
----------------------

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28    buildToolsVersion "29.0.2"    defaultConfig {
        applicationId "com.xyz.xyzratebook"        minSdkVersion 25        targetSdkVersion 28        versionCode 4        versionName '4.0'        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"        packagingOptions {
            // exclude unused native so file, ARMEABI has been removed in NDK r17.            exclude "lib/x86/**"            exclude "lib/x86_64/**"            exclude "lib/mips/**"            exclude "lib/mips64/**"            exclude "lib/arm64-v8a/**"        }
    }
    buildTypes {
        release {
            minifyEnabled false            shrinkResources false            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'    testImplementation 'junit:junit:4.12'    androidTestImplementation 'androidx.test.ext:junit:1.1.1'    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'    implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'}
3. replace all codes in MainActivity.java and rename the pdf file name
------------------------


package com.abc.book;
import androidx.appcompat.app.AppCompatActivity;import androidx.appcompat.app.AppCompatActivity;import com.github.barteksc.pdfviewer.PDFView;import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;import com.github.barteksc.pdfviewer.util.FitPolicy;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //PDF View        PDFView pdfView = findViewById(R.id.pdfView);        pdfView.fromAsset("book.pdf") //your pdf file in the assets folder
                //.pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default                .enableSwipe(true) // allows to block changing pages using swipe                .swipeHorizontal(false)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens                .spacing(0) // spacing between pages in dp. To define spacing color, set view background                .autoSpacing(true) // add dynamic spacing to fit each page on its own on the screen                //.linkHandler(DefaultLinkHandler)                .pageFitPolicy(FitPolicy.WIDTH) // mode to fit pages in the view                .fitEachPage(true) // fit each page to the view, else smaller pages are scaled relative to largest page.                .pageSnap(false) // snap pages to screen boundaries                .pageFling(false) // make a fling change only a single page like ViewPager                .nightMode(false) // toggle night mode                .scrollHandle(new DefaultScrollHandle(this))
                .load();    }
}


4. Replace all codes in activity_main.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"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">
    <com.github.barteksc.pdfviewer.PDFView        android:id="@+id/pdfView"        android:layout_width="match_parent"        android:layout_height="match_parent"/></androidx.constraintlayout.widget.ConstraintLayout>

Comments

Popular posts from this blog

Extracting and using a modified VMWare Player BIOS or UEFI firmware

Activate Synology Active Backup for Business for DSM 6

Windows 11 internet/account bypass