How to remove a button from toolbars?

You can easily edit PSPDFKit’s toolbars before they are displayed. For example, we’ll show how to remove the ink highlighter button from the annotation creation toolbar:

Copy
CustomActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setOnContextualToolbarLifecycleListener(this)
}

override fun onPrepareContextualToolbar(toolbar: ContextualToolbar<*>) {
    if (toolbar is AnnotationCreationToolbar) {
        // Get the existing menu items so we can modify them.
        val menuItems = toolbar.menuItems
            
        // Find and remove the ink highlighter button from the toolbar.
        val highlighterItem = menuItems.find { it.id ==  R.id.pspdf__annotation_creation_toolbar_item_ink_highlighter}
        if (highlighterItem != null) {
            menuItems.remove(highlighterItem)
        }

        // Replace the menu items.
        toolbar.setMenuItems(menuItems)
    }
}

override fun onDisplayContextualToolbar(toolbar: ContextualToolbar<*>) {}
override fun onRemoveContextualToolbar(toolbar: ContextualToolbar<*>) {}