Customizing the text selection popup toolbar on Android
On text selection, PdfFragment shows the TextSelectionPopupToolbar, a popup anchored to the selection with actions such as copy, speak, share, search, highlight, and redact. Unlike the annotation, document editing, and content editing toolbars, it isn’t a docked bar and isn’t driven by the ToolbarCoordinator. It has its own, smaller customization surface.
Customizing the actions
Register an OnPreparePopupToolbarListener on the fragment and change menuItems in onPrepareTextSelectionPopupToolbar. It runs every time the popup is about to show:
pdfFragment.setOnPreparePopupToolbarListener(object : OnPreparePopupToolbarListener { override fun onPrepareTextSelectionPopupToolbar(toolbar: TextSelectionPopupToolbar) { // Remove the share action and add your own. toolbar.menuItems = toolbar.menuItems.filter { it.id != R.id.pspdf__text_selection_toolbar_item_share } + PopupToolbarMenuItem(R.id.custom_text_action, R.string.custom_text_action, R.drawable.ic_my_action)
toolbar.setOnPopupToolbarItemClickedListener { item -> if (item.id == R.id.custom_text_action) { doSomething(pdfFragment.textSelection?.text); true } else false } }})getPdfFragment().setOnPreparePopupToolbarListener(new OnPreparePopupToolbarListener() { @Override public void onPrepareTextSelectionPopupToolbar(@NonNull TextSelectionPopupToolbar toolbar) { final List<PopupToolbarMenuItem> items = new ArrayList<>(toolbar.getMenuItems()); items.removeIf(item -> item.getId() == R.id.pspdf__text_selection_toolbar_item_share); items.add(new PopupToolbarMenuItem(R.id.custom_text_action, R.string.custom_text_action, R.drawable.ic_my_action)); toolbar.setMenuItems(items);
toolbar.setOnPopupToolbarItemClickedListener(item -> { if (item.getId() == R.id.custom_text_action) { doSomething(); return true; } return false; }); }});Returning true from the click listener marks the tap as handled; returning false makes the SDK run its default action for that item. Every default action has an ID of the form R.id.pspdf__text_selection_toolbar_item_* — for example, _copy, _speak, _share, _search, _highlight, _underline, _strikeout, _redact, _link, and _annotate.
To change a default action’s icon, replace the item with a copy carrying your drawable. A PopupToolbarMenuItem owns its icon, and the SDK has no per-action theme attributes:
toolbar.menuItems = toolbar.menuItems.map { item -> if (item.id == R.id.pspdf__text_selection_toolbar_item_copy) item.copy(iconRes = R.drawable.my_copy_icon) else item}Theming
The popup reads the pspdf__popupToolbarStyle theme attribute:
<item name="pspdf__popupToolbarStyle">@style/MyPopupToolbar</item>
<style name="MyPopupToolbar" parent="PSPDFKit.PopupToolbar"> <item name="pspdf__backgroundColor">@color/my_popup_background</item> <item name="pspdf__itemTint">@color/my_icon_color</item> <item name="pspdf__itemTintDisabled">@color/my_icon_color_disabled</item></style>Behavior
- The popup stays inside a popup window, so UI tests match it with
onView(withId(R.id.pspdf__text_selection_toolbar)).inRoot(isPlatformPopup()). - Immersive mode stays active while the popup is shown.
- Text-to-speech is one of the default actions; see text-to-speech to remove it.
For selecting text programmatically and observing selection changes, see the text selection guide. PopupToolbarCustomItemsExample in the Catalog app customizes this popup together with the annotation, long-press, and content editing popups.