Customizing Menus

You can modify the menus of PSPDFKit adding new or removing existing actions.

Modifying the action bar

If you are using the PdfActivity or a custom subclass of this activity PSPDFKit will automatically show toolbar actions based on the loaded configuration. PSPDFKit uses the default support Toolbar widget together with a dynamically populated Menu.

Default actions: Annotation editing, outline, search, sharing, thumbnails, overflow menu.

Adding custom actions

To add custom actions to the existing actions you need to use a custom activity class and override the #onPrepareOptionsMenu method:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CustomActivity : PdfActivity() {
    public override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        // Call the super implementation, so PSPDFKit can add it's own icons to the menu. This has
        // to be called first, or your own menu items won't appear.
        super.onPrepareOptionsMenu(menu)

        // Add your custom actions to the menu. We recommend to inflate them from a menu resource,
        // but you could also create them programmatically.
        menuInflater.inflate(R.menu.custom_menu, menu)

        // Make sure to return true, if you want the ActionBar to display your actions.
        return true
    }
}
Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class CustomActivity extends PdfActivity {
    @Override public boolean onPrepareOptionsMenu(Menu menu) {
        // Call the super implementation, so PSPDFKit can add it's own icons to the menu. This has
        // to be called first, or your own menu items won't appear.
        super.onPrepareOptionsMenu(menu);

        // Add your custom actions to the menu. We recommend to inflate them from a menu resource,
        // but you could also create them programmatically.
        getMenuInflater().inflate(R.menu.custom_menu, menu);

        // Make sure to return true, if you want the ActionBar to display your actions.
        return true;
    }
}

Note that your activity has to override the #onPrepareOptionsMenu method (not the #onCreateOptionsMenu method) in order to make the activity show the actions.

Adding action to the share menu

By overriding the #onPrepareOptionsMenu method you can also add actions to submenus, for example to the share menu. To do so, first fetch the menu item via its ID R.id.pspdf__menu_option_share_menu and add your action to its submenu.

Copy
1
2
val shareMenu = menu.findItem(R.id.pspdf__menu_option_share_menu).subMenu
shareMenu.add(0, R.id.my_custom_action, 0, "My custom share action")
Copy
1
2
final SubMenu shareMenu = menu.findItem(R.id.pspdf__menu_option_share_menu).getSubMenu();
shareMenu.add(0, R.id.my_custom_action, 0, "My custom share action");