Using a Fragment

In this document
chevron_rightToolbar menu

All actions related to the PDF viewer are handled through the PdfViewCtrlTabHostFragment fragment. This fragment extends and is responsible for showing document in tabs.

Please note that PdfViewCtrlTabHostFragment requires the com.pdftron:tools package.

First, make sure the following is in your <application> tag in AndroidManifest.xml.

        android:windowSoftInputMode="adjustPan" />
PdfViewCtrlTabHostFragment uses the AppCompat theme for material colors. Make sure that the value of android:theme in your activity tag also extends the AppCompat theme.

Displaying the PdfViewCtrlTabHostFragment in an activity is straightforward. Like any other fragment you should make an instance of the class and attach it to the activity. To create an instance of the host fragment you need to pass in a bundle that can be created using createBasicPdfViewCtrlTabBundle. The bundle specifies the document that should be shown when the host fragment is visible. For example, to open a document in the host fragment when you know the URI of the file (and its password if the file is password protected) you can call:

PdfViewCtrlTabHostFragment mPdfViewCtrlTabHostFragment;

Bundle args = PdfViewCtrlTabFragment.createBasicPdfViewCtrlTabBundle(context, fileUri, password);
mPdfViewCtrlTabHostFragment = PdfViewCtrlTabHostFragment.newInstance(args);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(, mPdfViewCtrlTabHostFragment, null);

You can simply convert a file to a URI by:

File file = ...
Uri fileUri.fromFile(file);

Note that since we use the support version of android fragment, your activity must extend FragmentActivity and call getSupportFragmentManager() to get the FragmentManager.

If you would like to customize the UI of PdfViewCtrlTabHostFragment, you can use ViewerConfig builder. For example:

ViewerConfig.Builder builder = new ViewerConfig.Builder();
ViewerConfig config = builder
    .toolbarTitle("Host Fragment")

Bundle args = PdfViewCtrlTabFragment.createBasicPdfViewCtrlTabBundle(context, fileUri, password, config);
args.putParcelable(PdfViewCtrlTabHostFragment.BUNDLE_TAB_HOST_CONFIG, config);
mPdfViewCtrlTabHostFragment = PdfViewCtrlTabHostFragment.newInstance(args);

For details on customizing the UI interface and sample usage of ViewerConfig builder, check out the configuration tutorial.

Toolbar menu

The default toolbar menu consists of menu items for

  • searching text in the current document
  • sharing the current document
  • setting up viewing mode
  • showing/hiding annotation toolbar
  • overflow menu

However, you can fully customize and control the toolbar menu as well as the navigation icon.

Customize toolbar menu

In order to change the default toolbar menu items you need to create your own menu in menu resource folder and pass it to the host fragment through bundle:


Customize navigation icon

To change the navigation icon, similar to the toolbar menu, you need to pass the desired icon to the host fragment through the bundle:

args.putInt(PdfViewCtrlTabHostFragment.BUNDLE_TAB_HOST_NAV_ICON, R.drawable.ic_arrow_back_white_24dp);

Interact with fragment

If you want to interact with the host fragment you should call addHostListener(TabHostListener) and override those methods that you are interested in. For example, you may want to override onToolbarOptionsItemSelected(MenuItem) when you add a new menu item, so when the item is clicked you can get a callback. As another example, you can get the callback when the navigation icon is clicked if you override onNavButtonPressed().

Here's an example that replaces the default navigation icon, and only keeps the annotation toolbar icon in the toolbar menu while adding a new menu item to show a toast when selected:

args.putInt(PdfViewCtrlTabHostFragment.BUNDLE_TAB_HOST_NAV_ICON, R.drawable.ic_arrow_back_white_24dp);
mPdfViewCtrlTabHostFragment = PdfViewCtrlTabHostFragment.newInstance(args);

public boolean onToolbarOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == {
        Toast.makeText(context, "Show toast is clicked!", Toast.LENGTH_SHORT).show();
    return false;

public void onNavButtonPressed() {
    // called when navigation button has been clicked

protected void onDestroy() {
    if (mPdfViewCtrlTabHostFragment != null) {

The fragment_viewer_simple.xml should be placed in menu resource folder and contain:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""
        android:title="Show toast"
        app:showAsAction="never" />

With this complete, you will see: