How to view document in Android with DocumentActivity
This section will show you how to open a document in a DocumentActivity. This is an AppCompatActivity which contains a PdfViewCtrlTabHostFragment2 and packages all the standard features used during document viewing. You can learn more in our document viewer overview.
The
AndroidManifest.xmlfile needs to be updated to request permissions to allowDocumentActivityto function properly. You also need to add some attributes to the<application>tag as well as a new<activity>tag to define theDocumentActivity. For more, see detailed description of permission requirements.
<?xml version="1.0" encoding="utf-8"?>
<!-- Include existing attributes in manifest -->
<manifest>
<!-- Required permissions are added here -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- Add multidex support and enable largeHeap. Also enable usesCleartextTraffic in application attributes if you are working with HTTP files. If you are only working with HTTPS files, this is not required. -->
<!-- Include existing attributes in application -->
<application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="false">
<!-- Add license key in meta-data tag here. This should be inside the application tag. -->
<!-- This is from the Integration stage-->
<meta-data android:name="pdftron_license_key" android:value="${pdftronLicenseKey}" />
<!-- Document viewer activity declaration-->
<activity android:name="com.pdftron.pdf.controls.DocumentActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
</application>
</manifest>Storage Permission
Please follow the latest Android best practices and guidelines.
Warn
If your app targets Android SDK version 28 or higher, add the android:usesCleartextTraffic="true" attribute in your application tag to open HTTP files in the viewer. If you're only working with HTTPS files, this is not required.
Import the following in your MainActivity.java file.
Inside the
onCreatemethod for yourMainActivityclass, add the following lines to open a demo PDF from a URL. You can open documents usingDocumentActivityin many ways.
Your app should look something like this:

You can explore the features of the viewer and also many customization options.
More loading options
DocumentActivity can open documents from various resources including internal storage, URIs, web servers and the application's res folder.
You can open files from the these sources using the code below:
Next step
Last updated
Was this helpful?